Generate Xml for <Govtalkmessage Xmlns="Http://Www.Govtalk.Gov.Uk/Cm/Envelope">

Using TSQL and XQuery to extract values from XML

A couple ways..

DECLARE @X XML = '
<GovTalkMessage xmlns="http://www.govtalk.gov.uk/CM/envelope">
<EnvelopeVersion>2.0</EnvelopeVersion>
</GovTalkMessage>';

SELECT @X.value('(//*:EnvelopeVersion/text())[1]', 'varchar(20)');

Or..

DECLARE @X VARCHAR(1000) = '
<GovTalkMessage xmlns="http://www.govtalk.gov.uk/CM/envelope">
<EnvelopeVersion>2.0</EnvelopeVersion>
</GovTalkMessage>';

SELECT CAST(@X AS XML).value('(//*:EnvelopeVersion/text())[1]', 'varchar(20)');

manipulation of xml using c#

//Load the XML
XmlDocument documentXML = new XmlDocument();
documentXML.Load(Server.MapPath("AddDeleteUpdate.xml"));

XmlNamespaceManager xmlns = new XmlNamespaceManager(documentXML.NameTable);
xmlns.AddNamespace("bk", "http://www.govtalk.gov.uk/CM/envelope");

//Identify the parent node i.e <MessageDetails>
XmlNode nodeMessage = documentXML.SelectSingleNode("//bk:GovTalkMessage/bk:Header/bk:MessageDetails", xmlns);

//Delete the node.
XmlNode nodeTransactionID = documentXML.SelectSingleNode("//bk:GovTalkMessage/bk:Header/bk:MessageDetails/bk:TransactionID", xmlns);
nodeMessage.RemoveChild(nodeTransactionID);

//Create the new XML noded to be added.
XmlNode controlAttrNode = null;
controlAttrNode = documentXML.CreateElement("Transformation");
controlAttrNode.InnerText = "XML";
controlAttrNode.Attributes.RemoveAll();

//Get the node object to where it need to be added.
XmlNode nodeCorrelation = documentXML.SelectSingleNode("//bk:GovTalkMessage/bk:Header/bk:MessageDetails/bk:CorrelationID", xmlns);
//Insert the node after.
nodeMessage.InsertAfter(controlAttrNode, nodeCorrelation);

documentXML.Save(Server.MapPath("AddDeleteUpdate.xml"));

manipulation of xml using c#

//Load the XML
XmlDocument documentXML = new XmlDocument();
documentXML.Load(Server.MapPath("AddDeleteUpdate.xml"));

XmlNamespaceManager xmlns = new XmlNamespaceManager(documentXML.NameTable);
xmlns.AddNamespace("bk", "http://www.govtalk.gov.uk/CM/envelope");

//Identify the parent node i.e <MessageDetails>
XmlNode nodeMessage = documentXML.SelectSingleNode("//bk:GovTalkMessage/bk:Header/bk:MessageDetails", xmlns);

//Delete the node.
XmlNode nodeTransactionID = documentXML.SelectSingleNode("//bk:GovTalkMessage/bk:Header/bk:MessageDetails/bk:TransactionID", xmlns);
nodeMessage.RemoveChild(nodeTransactionID);

//Create the new XML noded to be added.
XmlNode controlAttrNode = null;
controlAttrNode = documentXML.CreateElement("Transformation");
controlAttrNode.InnerText = "XML";
controlAttrNode.Attributes.RemoveAll();

//Get the node object to where it need to be added.
XmlNode nodeCorrelation = documentXML.SelectSingleNode("//bk:GovTalkMessage/bk:Header/bk:MessageDetails/bk:CorrelationID", xmlns);
//Insert the node after.
nodeMessage.InsertAfter(controlAttrNode, nodeCorrelation);

documentXML.Save(Server.MapPath("AddDeleteUpdate.xml"));

C# can't deserialize XML containing xsi:type

You need to change the decoration on the returnDRUG class

from this

[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.govtalk.gov.uk/CM/envelope")]
public partial class returnDRUG

to this

[System.Xml.Serialization.XmlTypeAttribute(TypeName = "genericDrug", Namespace = "http://webservice.sirkb/")]
public partial class returnDRUG

Specify it's type as "genericDrug", and the crucial bit, correct it's namespace to "http://webservice.sirkb/"

I've just used you code and managed to de-serialize using this change.

The explanation is that if you take a look at the definition of DRUG you can see that it's type is defined as "genericDRUG" in the namespace alias "ns2"

<DRUG xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns2:genericDrug">

if you look at the definition of the ns2 alias you can see that it's "http://webservice.sirkb/"

<ns2:getGenericDrugsResponse xmlns:ns2="http://webservice.sirkb/">

Some addition to prevent default values for DRUG objects

Every property of the returnDrug class should have the namespace http://www.govtalk.gov.uk/CM/envelope.

The complete class should have the following form:

[System.Xml.Serialization.XmlTypeAttribute(TypeName = "genericDrug", Namespace = "http://webservice.sirkb/")]
public partial class returnDRUG
{

private string dOSAGEField;

private ushort gENERIC_DRUG_IDField;

private string gENERIC_DRUG_NAMEField;

private string pHARMACEUTICAL_FORMField;

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Namespace = "http://www.govtalk.gov.uk/CM/envelope")]
public string DOSAGE
{
get
{
return this.dOSAGEField;
}
set
{
this.dOSAGEField = value;
}
}

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Namespace = "http://www.govtalk.gov.uk/CM/envelope")]
public ushort GENERIC_DRUG_ID
{
get
{
return this.gENERIC_DRUG_IDField;
}
set
{
this.gENERIC_DRUG_IDField = value;
}
}

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Namespace = "http://www.govtalk.gov.uk/CM/envelope")]
public string GENERIC_DRUG_NAME
{
get
{
return this.gENERIC_DRUG_NAMEField;
}
set
{
this.gENERIC_DRUG_NAMEField = value;
}
}

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Namespace = "http://www.govtalk.gov.uk/CM/envelope")]
public string PHARMACEUTICAL_FORM
{
get
{
return this.pHARMACEUTICAL_FORMField;
}
set
{
this.pHARMACEUTICAL_FORMField = value;
}
}
}

Problems translating XML with XSLT

Since you haven't provided the rules by which this transformation should occur (e.g., are there ever more than one search row?), here's a short, rather unintelligently-produced XSLT that accomplishes what you ask.

When this XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:x="http://www.govtalk.gov.uk/CM/envelope"
xmlns:t="http://xmlgw.companieshouse.gov.uk/v1-0/schema"
exclude-result-prefixes="x t"
version="1.0">
<xsl:output omit-xml-declaration="no" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="/*">
<CompanySearchResult xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<RegistrationNumber>
<xsl:value-of select="x:Body/*/*/t:CompanyNumber" />
</RegistrationNumber>
<RegisteredName>
<xsl:value-of select="x:Body/*/*/t:CompanyName" />
</RegisteredName>
</CompanySearchResult>
</xsl:template>

</xsl:stylesheet>

...is applied to the originally provided XML:

<?xml version="1.0" encoding="UTF-8"?>
<GovTalkMessage xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:gt="http://www.govtalk.gov.uk/schemas/govtalk/core" xmlns:dsig="http://www.w3.org/2000/09/xmldsig#" xmlns="http://www.govtalk.gov.uk/CM/envelope" xsi:schemaLocation="http://www.govtalk.gov.uk/CM/envelope http://xmlgw.companieshouse.gov.uk/v1-0/schema/Egov_ch-v2-0.xsd">
<EnvelopeVersion>1.0</EnvelopeVersion>
<Header>
<MessageDetails>
<Class>NumberSearch</Class>
<Qualifier>response</Qualifier>
<TransactionID>4c5cf4a9e1a44cbbbe800ad9ea9f06fd</TransactionID>
<GatewayTimestamp>2012-09-27T18:34:19-00:00</GatewayTimestamp>
</MessageDetails>
<SenderDetails>
<IDAuthentication>
<SenderID>XMLGatewayTestUserID</SenderID>
<Authentication>
<Method>CHMD5</Method>
<Value/>
</Authentication>
</IDAuthentication>
</SenderDetails>
</Header>
<GovTalkDetails>
<Keys/>
</GovTalkDetails>
<Body>
<NumberSearch xmlns="http://xmlgw.companieshouse.gov.uk/v1-0/schema" xsi:schemaLocation="http://xmlgw.companieshouse.gov.uk/v1-0/schema http://xmlgw.companieshouse.gov.uk/v1-0/schema/NumberSearch.xsd">
<SearchRows>1</SearchRows>
<CoSearchItem>
<CompanyName>MILLENNIUM STADIUM PLC</CompanyName>
<CompanyNumber>03176906</CompanyNumber>
<DataSet>LIVE</DataSet>
<CompanyIndexStatus/>
<CompanyDate/>
</CoSearchItem>
</NumberSearch>
</Body>
</GovTalkMessage>

...the wanted result is produced:

<?xml version="1.0"?>
<CompanySearchResult xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<RegistrationNumber>03176906</RegistrationNumber>
<RegisteredName>MILLENNIUM STADIUM PLC</RegisteredName>
</CompanySearchResult>

Note the correct usage of the two namespaces needed to make this transformation work. I believe yours fails because you only specify one namespace (which, for the <CompanyNumber> and the <CompanyName> elements, is the incorrect one).

Unable to use xpath as expected on document

XPath needs namespaces. The tags you are trying to select are in one, so you have to tell the select where to look.



Related Topics



Leave a reply



Submit