C # create an XML document

Source: Internet
Author: User
Tags biztalk

Sometimes we need to generate an xml document as a container for data exchange. Of course, we use the concatenation string method to construct xml, but this method is simple and effective, but if the structure of the xml document is too complicated, the concatenation string will be dazzled. At this time, we need C # To provide us with ready-made class libraries for us to freely create xml documents.

For example, we want to create the following xml document

 
        
  
   ZFM1
      
          
   
    13022101
           
   
    2013238955
           
   
    4140
       
      
          
   
    13022101
           
   
    2013239627
           
   
    4140
       
  
 

This structure is selected, on the one hand, because it comes from the actual needs of a forum user, on the other hand, it is simple enough but representative.

The following describes how to use XmlDocument and Linq to XML in this example.

1. XmlDocument

XmlDocument document = new XmlDocument (); XmlDeclaration declaration = document. createXmlDeclaration ("1.0", "UTF-8", ""); // declaration part of the xml document. appendChild (declaration); XmlElement root = document. createElement ("ns0", "Z_AVS_UPLOAD_WEIGHT_Request", "http://schemas.microsoft.com/BizTalk/2003"); document. appendChild (root); XmlElement zwerks = document. createElement ("ZWERKS"); zwerks. innerText = "ZFM1"; root. appendChild (zwerks); XmlElement tab1 = document. createElement ("TAB1"); root. appendChild (tab1); XmlElement zno = document. createElement ("ZNO"); zno. innerText = "13022101"; tab1.AppendChild (zno); XmlElement zorder = document. createElement ("ZORDER"); zorder. innerText = "2013238955"; tab1.AppendChild (zorder); XmlElement zweight = document. createElement ("ZWEIGHT"); zweight. innerText = "4140"; tab1.AppendChild (zweight); XmlElement tab2 = document. createElement ("TAB1"); root. appendChild (tab2); XmlElement zno2 = document. createElement ("ZNO"); zno2.InnerText = "13022101"; tab2.AppendChild (zno2); XmlElement zorder2 = document. createElement ("ZORDER"); zorder2.InnerText = "2013238955"; tab2.AppendChild (zorder2); XmlElement zweight2 = document. createElement ("ZWEIGHT"); zweight2.InnerText = "4140"; tab2.AppendChild (zweight2); document. save ("test. xml "); // Save the generated xml to test. xml file
2. Linq to XML
XDocument document = new XDocument (); document. declaration = new XDeclaration ("1.0", "UTF-8", ""); XNamespace ns = "http://schemas.microsoft.com/BizTalk/2003"; XElement root = new XElement (ns + "Z_AVS_UPLOAD_WEIGHT_Request ", new XAttribute (XNamespace. xmlns + "ns0", "http://schemas.microsoft.com/BizTalk/2003"); root. add (new XElement ("ZWERKS", "ZFM1"), new XElement ("TAB1", new XElement ("ZNO", 13022101), new XElement ("ZORDER ", 2013238955), new XElement ("ZWEIGHT", 4140), new XElement ("TAB1", new XElement ("ZNO", 13022101), new XElement ("ZORDER ", 2013238955), new XElement ("ZWEIGHT", 4140); document. add (root); document. save ("test. xml "); // save xml to a file

It can be found that the method of Linq to XML is relatively simple, and the amount of code is small enough. Of course, the XmlDocument method can be further simplified. Here we will focus on the charm of Linq to XML.

Haha, the above is just to provide some ideas and solutions to friends in need. For more information about Linq to xml, see XNamespace.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.