简体   繁体   中英

Create XML file using c# like below format

I need to create xml using c# below format.

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
    <SOAP-ENV:Body>
        <AddressLine1 xmlns="">50 W TOWN ST</AddressLine1>
        <AddressLine2 xmlns="">STE 400</AddressLine2>
        <City xmlns="">COLUMBUS</City>
        <State xmlns="">OH</State>
        <Zip xmlns="">43215</Zip>
        <Zip4 xmlns=""/>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope> 

Below is my c# code to create xml like above

        XmlDocument doc = new XmlDocument();
        XmlNode docNode = doc.CreateElement("SOAP-ENV:Envelope");
        XmlAttribute typeAttr = doc.CreateAttribute("xmlns:SOAP-ENV");
        typeAttr.Value = "http://schemas.xmlsoap.org/soap/envelope/";
        docNode.Attributes.Append(typeAttr);
        doc.AppendChild(docNode);

        XmlNode AddressRequestBody = doc.CreateElement("SOAP-ENV:Body");
        docNode.AppendChild(AddressRequestBody);

        ........
        ........

        XmlNode Zip4Node = doc.CreateElement("Zip4");
        typeAttr = doc.CreateAttribute("xmlns");
        typeAttr.Value = "";
        Zip4Node.Attributes.Append(typeAttr);
        Zip4Node.AppendChild(doc.CreateTextNode(""));
        AddressRequestBody.AppendChild(Zip4Node);

With the above code I am geting xml like below. missing SOAP-ENV: in Envelop and body tags. Any idea to how to get SOAP-ENV: in both Envelop and body tags. I am new to xml not sure how to get.

<Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
 <Body>
  <AddressLine1 xmlns="">Add1</AddressLine1>
  <AddressLine2 xmlns="">Add2</AddressLine2>
  <City xmlns="">City1</City>
  <State xmlns="">state1</State>
  <Zip xmlns="">zip1</Zip>
  <Zip4 xmlns=""></Zip4>
 </Body>
</Envelope>

You can create your required XML using the XML Document Object Model as follows:

var soapNs = @"http://schemas.xmlsoap.org/soap/envelope/";
var bodyNs = @"";

var doc = new XmlDocument();
var root = doc.AppendChild(doc.CreateElement("SOAP-ENV", "Envelope", soapNs));
var body = root.AppendChild(doc.CreateElement("SOAP-ENV", "Body", soapNs));

body.AppendChild(doc.CreateElement("", "AddressLine1", bodyNs)).InnerText = "50 W TOWN ST";
body.AppendChild(doc.CreateElement("", "AddressLine2", bodyNs)).InnerText = "STE 400";
body.AppendChild(doc.CreateElement("", "City", bodyNs)).InnerText = "COLUMBUS";
body.AppendChild(doc.CreateElement("", "State", bodyNs)).InnerText = "OH";
body.AppendChild(doc.CreateElement("", "Zip", bodyNs)).InnerText = "43215";
body.AppendChild(doc.CreateElement("", "Zip4", bodyNs)).InnerText = "";

Using this approach, the following XML is generated:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
  <SOAP-ENV:Body>
    <AddressLine1>50 W TOWN ST</AddressLine1>
    <AddressLine2>STE 400</AddressLine2>
    <City>COLUMBUS</City>
    <State>OH</State>
    <Zip>43215</Zip>
    <Zip4></Zip4>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

Notes:

  1. A well-formed XML document must have one and only one root element . In the initial version of your question, you try to add multiple nodes directly to the XmlDocument doc :

     doc.AppendChild(docNode); // Snip some code doc.AppendChild(AddressRequestNode); 

    Attempting to do this causes the This document already has a 'DocumentElement' node. exception to be thrown.

  2. Your XML document has elements in two namespaces: http://schemas.xmlsoap.org/soap/envelope/ and the empty namespace. In situations such as this, it's easiest to use the XmlDocument.CreateElement(string prefix, string localName, string namespaceURI) API and always explicitly specify the namespace URI, rather than simply specifying the namespace lookup prefix and hoping you had correctly initialized the corresponding xmlns: prefix value earlier.

    In the sample XML, the Envelope and Body nodes are in the http://schemas.xmlsoap.org/soap/envelope/ namespace while their child nodes are in the empty namespace, which is what is reflected in my sample code.

  3. XmlNode.AppendChild(XmlNode) returns the appended child and so can be used in a fluent style.

  4. In the sample XML the innermost elements are explicitly specified to have a default namespace of "" :

     <AddressLine1 xmlns="">50 W TOWN ST</AddressLine1> 

    Actually the xmlns="" is unnecessary because there is no default namespace specified at a higher level in the XML. Thus the following is semantically identical and sufficient:

     <AddressLine1>50 W TOWN ST</AddressLine1> 

    If for some reason you are required to have this redundant namespace declaration, you can do:

     var line1 = body.AppendChild(doc.CreateElement("", "AddressLine1", bodyNs)); line1.Attributes.Append(doc.CreateAttribute("xmlns")).Value = bodyNs; line1.InnerText = "50 W TOWN ST"; 

    Which results in

     <AddressLine1 xmlns="">50 W TOWN ST</AddressLine1> 
  5. XmlElement.InnerText can be used to set the text value of a element with no child elements. This is more succinct than calling CreateTextNode() then appending the result to the DOM.

Working sample .Net fiddle here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM