简体   繁体   中英

How to format XML using XmlWriter in C#.Net?

I want the XML in format like :

  <?xml version="1.0" encoding="UTF-8"?>
  <ac:Main xmlns:trip="http://www.test.com/main.xsd"
     xmlns:abc="http://www.test.com/Types.xsd"
     xmlns:tw="http://www.test.com/TW.xsd"
     xmlns:ck="http://www.test.com/CK.xsd"
     xmlns:k1="http://www.test.com/K1.xsd"
     xmlns:d1="http://www.test.com/D1.xsd"
     xmlns:ac="http://www.test.com/Ac.xsd"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://www.test.com/Ac.xsd file:/D:/schema/AC.xsd">
        <ac:Records>
            <d1:Header>
           <abc:FirstElement>firstValue</abc:FirstElement>
        </d1:Header>
       </ac:Records>
     </ac:Main>

So, I wrote code using XmlWriter as following:

      using (XmlWriter writer = XmlWriter.Create(path, settings))
        {
            writer.WriteStartElement("ac", "Main", "xmlns");
            writer.WriteAttributeString("xmlns", "trip", null,  http://www.test.com/main.xsd");
            writer.WriteAttributeString("xmlns", "abc", null,    http://www.test.com/Types.xsd");
            writer.WriteAttributeString("xmlns", "tw", null, http://www.test.com/TW.xsd");
            writer.WriteAttributeString("xmlns", "kc", null, "http://www.test.com/CK.xsd");
            writer.WriteAttributeString("xmlns", "k1", null, "http://localhost:8080/K1.xsd");
            writer.WriteAttributeString("xmlns", "d1", null, "http://localhost:8080/D1.xsd");                  
            writer.WriteAttributeString("xsi","schemaLocation", "http://www.w3.org/2001/XMLSchema-instance", "http://www.test.com/Ac.xsd file:/D:/schema/AC.xsd");

           // writer.WriteAttributeString("xmlns", "ac", null, "http://www.test.com/Ac.xsd ");

            writer.WriteStartElement("ac", "Records", "xmlns");  
            writer.WriteStartElement("d1", "Header", "xmlns");

            writer.WriteStartElement("abc", "FirstElement", "xmlns");
            writer.WriteString("firstValue");
            writer.WriteEndElement(); 
            writer.WriteEndElement(); 
            writer.WriteEndElement(); 
        }

But uncommenting line writer.WriteAttributeString("xmlns", "ac", null, "http://www.test.com/Ac.xsd "); yields error "The prefix 'ac' cannot be redefined from 'xmlns' to 'http://www.test.com/Ac.xsd ' within the same start element tag."

So, I commment that line as shown in above code and I got output as :

 <?xml version="1.0" encoding="utf-8"?>
    <ac:Main 
     xmlns:wctrp="http://www.test.com/main.xsd"
     xmlns:abc="http://www.test.com/Types.xsd"
     xmlns:tw="http://www.test.com/TW.xsd"
     xmlns:ck="http://www.test.com/CK.xsd"
     xmlns:k1="http://www.test.com/K1.xsd"
     xmlns:d1="http://www.test.com/D1.xsd"
     xmlns:ac="http://www.test.com/Ac.xsd"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://www.test.com/Ac.xsd file:/D:/schema/AC.xsd" 
     xmlns:ac="xmlns">
      <ac:Records>
            <d1:Header xmlns:hd1="xmlns">
        <abc:FirstElement  xmlns:iaiabc="xmlns">firstValue</abc:FirstElement>
        </d1:Header>
      </ac:Records>
     </ac:Main>

The unwanted attibutes are xmlns:ac="xmlns" in element 'Main', xmlns:hd1="xmlns" in 'd1:Header' and
xmlns:iaiabc="xmlns" in 'abc:FirstElement'.

Any one kindly suggest me what should I do to remove the unwanted attibutes and to get the output in topmost format.

I am new to XmlWriter.

You're misusing WriteStartElement. Namespace is not supposed to be xmlns. It's supposed to be the actual namespace you're going to use. Also, because the first occurrence of ac comes before the attributes are written, do not include an xmlns attribute line for ac. The xmlns attribute for ac will get generated automatically by the writer.

You could actually leave out all the xmlns attribute lines, but then the namespaces would be declared on the first element where they occur.

    using (XmlWriter writer = XmlWriter.Create(path))
    {
        writer.WriteStartElement("ac", "Main", "http://www.test.com/Ac.xsd");
        writer.WriteAttributeString("xmlns", "trip", null, "http://www.test.com/main.xsd");
        writer.WriteAttributeString("xmlns", "abc", null, "http://www.test.com/Types.xsd");
        writer.WriteAttributeString("xmlns", "tw", null, "http://www.test.com/TW.xsd");
        writer.WriteAttributeString("xmlns", "kc", null, "http://www.test.com/CK.xsd");
        writer.WriteAttributeString("xmlns", "k1", null, "http://localhost:8080/K1.xsd");
        writer.WriteAttributeString("xmlns", "d1", null, "http://localhost:8080/D1.xsd");                  
        writer.WriteAttributeString("xsi","schemaLocation", "http://www.w3.org/2001/XMLSchema-instance", "http://www.test.com/Ac.xsd file:/D:/schema/AC.xsd");

        writer.WriteStartElement("ac", "Records", "http://www.test.com/Ac.xsd");  
        writer.WriteStartElement("d1", "Header", "http://localhost:8080/D1.xsd");

        writer.WriteStartElement("abc", "FirstElement", "http://www.test.com/Types.xsd");
        writer.WriteString("firstValue");
        writer.WriteEndElement(); 
        writer.WriteEndElement(); 
        writer.WriteEndElement(); 
    }

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