C # create xml

Source: Internet
Author: User

The following is the core generation segment, which is included in the xml file of the attachment.
XmlDocument xmlDocument = new XmlDocument (); // create xml document
XmlNode xmlNode = xmlDocument. CreateNode (XmlNodeType. XmlDeclaration, "", ""); // xml document header declaration
XmlDocument. AppendChild (xmlNode); // add xml document header declarations
XmlElement xmlElement = xmlDocument. CreateElement ("", "TestDataModels", ""); // create xml root node
XmlElement element = xmlDocument. CreateElement ("", "TestDataModels", ""); // create TestDataModels 'child node
For (int I = 1; I <6; I ++)
{
XmlElement element = xmlDocument. CreateElement ("", "TestDataModels", ""); // create TestDataModels 'child node
// Add child node for the TestDataModels node
XmlElement elementCode = xmlDocument. CreateElement ("Test", "Code ","");
XmlText xmlTextCode = xmlDocument. CreateTextNode (tm. TCode );
ElementCode. AppendChild (xmlTextCode );
Element. AppendChild (elementCode );

XmlElement elementName = xmlDocument. CreateElement ("Test", "Name ","");
XmlText xmlTextName = xmlDocument. CreateTextNode (tm. TName );
ElementName. AppendChild (xmlTextName );
Element. AppendChild (elementName );

// TestType start
XmlElement elementType = xmlDocument. CreateElement ("Test", "Type ","");
XmlElement elementTypeCode = xmlDocument. CreateElement ("Type", "TCode ","");
XmlText xmlTextTypeCode = xmlDocument. CreateTextNode (tt. TtCode );
ElementTypeCode. AppendChild (xmlTextTypeCode );
ElementType. AppendChild (elementTypeCode );

XmlElement elementTypeName = xmlDocument. CreateElement ("Type", "TName ","");
XmlText xmlTextTypeName = xmlDocument. CreateTextNode (tt. TtName );
ElementTypeName. AppendChild (xmlTextTypeName );
ElementType. AppendChild (elementTypeName );
Element. AppendChild (elementType );

// TestType end
XmlElement. AppendChild (element); // For xmlElement add child element
}
XmlDocument. AppendChild (xmlElement); // For xmlDocument add child node
XmlDocument. Save (dataFile); // save the xmlDocument

This article is from the "Wolf soul" blog

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.