Sometimes we need to generate an XML document as a container for data exchange. Of course, we use the concatenation string method to build the XML, but although this method is simple and effective, but if the XML document structure is too complex, the concatenation of strings can be confusing. This is where C # is required to provide us with a ready-made class library for us to freely create XML documents.
For example, we want to create the following XML document
<?XML version= "1.0" encoding= "UTF-8"?><ns0:z_avs_upload_weight_requestXMLNS:NS0= "http://schemas.microsoft.com/BizTalk/2003"> <Zwerks>ZFM1</Zwerks> <TAB1> <ZNO>13022101</ZNO> <ZORDER>2013238955</ZORDER> <Zweight>4140</Zweight> </TAB1> <TAB1> <ZNO>13022101</ZNO> <ZORDER>2013239627</ZORDER> <Zweight>4140</Zweight> </TAB1></ns0:z_avs_upload_weight_request>
The selection of such a structure, on the one hand, is because it comes from the actual needs of a forum friend, on the other hand, it is simple enough but representative.
I'll explain this in two ways (XmlDocument and LINQ to XML) in this example.
1, XmlDocument
XmlDocument document =NewXmlDocument (); XmlDeclaration Declaration= document. Createxmldeclaration ("1.0","UTF-8","");//declaration part of an XML documentdocument. 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 the Test.xml file
2. Linq to XML
XDocument document =NewXDocument (); Document. Declaration=NewXdeclaration ("1.0","UTF-8",""); XNamespace NS="http://schemas.microsoft.com/BizTalk/2003"; XElement Root=NewXElement (ns +"z_avs_upload_weight_request", NewXAttribute (Xnamespace.xmlns +"NS0","http://schemas.microsoft.com/BizTalk/2003")); Root. ADD (NewXElement ("Zwerks","ZFM1"), NewXElement ("TAB1", NewXElement ("ZNO",13022101), NewXElement ("ZORDER",2013238955), NewXElement ("Zweight",4140)), NewXElement ("TAB1", NewXElement ("ZNO",13022101), NewXElement ("ZORDER",2013238955), NewXElement ("Zweight",4140)) ); Document. ADD (root); Document. Save ("Test.xml");//save XML to File
The LINQ to XML method can be found to be concise and the code volume is small enough. Yes, of course. The XmlDocument approach can be further simplified, highlighting the charm of LINQ to XML.
Hehe, as above only to the need to provide friends with ideas and solutions. For more information on LINQ to XML, refer to: XNamespace.
C # Creating an XML document