XML file Operation class--Creating an XML file

Source: Internet
Author: User

This class is encapsulated on the Microsoft XML Operations Class Library, just for easier use, including examples of XML class creation nodes.

usingSystem;usingSystem.Collections;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Xml;namespacetestform{classoperation_appcfg {XmlDocument xmldoc;        XmlNode XmlNode;        XmlElement Xmlelem;        XmlDeclaration xmldecl; /// <summary>        ///constructor Function/// </summary>         Publicoperation_appcfg () {xmldoc=NewXmlDocument (); }        /// <summary>        ///create a paragraph declaration of an XML file/// </summary>        /// <param name= "strencoding" >XML encoding method, input "gb2312", "Utf-8"</param>        /// <param name= "Strstandalone" >Standalone attribute, enter "yes", "no" or "null", default to "null"</param>         Public voidCreatedeclaration (stringStrencoding,stringStrstandalone) {            if(Strencoding = =NULL) || (Strencoding = ="") ) {strencoding=NULL; }            if(Strstandalone = =NULL) || (Strstandalone = ="") ) {Strstandalone=NULL; }            Else            {                if(!strstandalone.equals ("Yes") || !strstandalone.equals ("No") ) {Strstandalone=NULL; }} xmldecl= xmldoc. Createxmldeclaration ("1.0", strencoding, Strstandalone); XmlDoc.        AppendChild (XMLDECL); }        /// <summary>        ///creating the root element/// </summary>        /// <param name= "LocalName" >the name of the root element</param>         Public voidCreateElement (stringlocalname) {Xmlelem= xmldoc. CreateElement ("", LocalName,""); XmlDoc.        AppendChild (Xmlelem); }        /// <summary>        ///Create child nodes of a node, pattern<node suba= "SubA" subb= "Subb" ></Node>        /// </summary>        /// <param name= "ParentName" >the name of the parent node</param>        /// <param name= "NodeName" >the name of the node</param>        /// <param name= "hash" >Hashtable of child nodes to be created</param>         Public voidCreatenodemodea (stringParentName,stringNodeName, Hashtable hash) {XmlNode root=xmldoc.            selectSingleNode (ParentName); XmlElement Xe1= xmldoc. CreateElement ("Node");//Create a <Node> node            foreach(DictionaryEntry deinchhash) {xe1. SetAttribute (DE. Key.tostring (), DE. Value.tostring ());//set the node genre property//Xe1. SetAttribute ("ISBN", "2-3631-4");} root.        AppendChild (XE1); }        /// <summary>        ///Create a node's child node (generic method), pattern<node suba= "SubA" subb= "Subb" ></Node>        /// </summary>        /// <param name= "ParentName" >the name of the parent node</param>        /// <param name= "NodeName" >the name of the node</param>        /// <param name= "hash" >Hashtable of child nodes to be created</param>         Public voidCreatenodemodeb (stringParentName,stringNodeName, dictionary<string,string>hash) {XmlNode root=xmldoc.            selectSingleNode (ParentName); XmlElement Xe1= xmldoc. CreateElement ("Node");//Create a <Node> node            foreach(keyvaluepair<string,string> deinchhash) {xe1. SetAttribute (DE. Key.tostring (), DE. Value.tostring ());//set the node genre property//Xe1. SetAttribute ("ISBN", "2-3631-4");} root.        AppendChild (XE1); }        /// <summary>        ///Create a node's child node (generic method), pattern<title>Getting started to mastering</title>        /// </summary>        /// <param name= "NodeName" >the name of the node</param>        /// <param name= "hash" >Hashtable of child nodes to be created</param>         Public voidCreatenodemodec (stringparentname,hashtable Hash) {XmlNode root=xmldoc.            selectSingleNode (ParentName);            XmlElement Xesub1; foreach(DictionaryEntry deinchhash) {Xesub1=xmldoc. CreateElement (DE.                Key.tostring ()); Xesub1. InnerText= de. Value.tostring ();//Set the text nodeRoot.            AppendChild (XESUB1); }        }        /// <summary>        ///Save File/// </summary>        /// <param name= "Pathfile" ></param>         Public voidSaveFile (stringpathfile) {xmldoc.        Save (Pathfile); }    }}

The following is an online example written by a netizen, reference the same meaning, the original address http://www.cnblogs.com/txw1958/archive/2013/01/16/csharp-xml.html

         Public voidCreateFile () {//join the declaration paragraph of XML, <?xml version= "1.0" encoding= "gb2312"?>XmlDeclaration xmldecl = xmldoc. Createxmldeclaration ("1.0",NULL,NULL);//encoding mode: "gb2312", "Utf-8", can also be nullxmldoc.            AppendChild (XMLDECL); //Add a root elementXmlelem = xmldoc. CreateElement ("","Configuration",""); XmlDoc.            AppendChild (Xmlelem); //add a different element             for(inti =1; I <3; i++) {XmlNode root= xmldoc. selectSingleNode ("Employees");//Find <Employees>XmlElement xe1 = xmldoc. CreateElement ("Node");//Create a <Node> nodeXe1. SetAttribute ("genre","Doucube");//set the node genre propertyXe1. SetAttribute ("ISBN","2-3631-4");//Set the Node ISBN propertyXmlElement xesub1= xmldoc. CreateElement ("title"); Xesub1. InnerText="CS from beginner to proficient";//Set the text nodeXe1. AppendChild (XESUB1);//Add to <Node> nodeXmlElement xesub2 = xmldoc. CreateElement ("author"); Xesub2. InnerText="waiting for Czech Republic"; Xe1.                AppendChild (XESUB2); XmlElement xesub3= xmldoc. CreateElement (" Price"); Xesub3. InnerText="58.3"; Xe1.                AppendChild (XESUB3); Root. AppendChild (XE1);//Add to <Employees> node            }            //Save the created XML documentXmlDoc. Save ("db.cfg"); }

App. Config is essentially an XML file, and here are some references

Http://www.cnblogs.com/zfanlong1314/p/3623622.html

Https://msdn.microsoft.com/zh-cn/library/system.configuration.configuration (v=vs.80). aspx

Http://www.cnblogs.com/bynet/archive/2010/06/10/1755721.html

Other references

http://blog.csdn.net/albertliangyg/article/details/8633521

Https://msdn.microsoft.com/zh-cn/library/system.text.encoding (v=vs.80). aspx

Https://msdn.microsoft.com/zh-cn/library/system.xml.xmldocument.createelement (v=vs.100). aspx

Https://msdn.microsoft.com/zh-cn/library/5tbh8a42

XML file Operation class--Creating an XML file

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.