C # Create attribute value pair data structure type XML file Tool class

Source: Internet
Author: User
      <summary>///Create XML file///</summary>///<param name= "filename" > file name created Called </param>///<param name= "nodename" > Node name collection </param>///<param name= "ValueList" > Properties

            Value pairs collection </param> public static void Createxmlfile (String filename,list<string>nodename,hashtable ht) {
            XmlDocument document = new XmlDocument (); Create type declaration node XmlNode nodes = document.
            Createxmldeclaration ("1.0", "Utf-8", ""); Document.
            AppendChild (node); Create root node XmlNode root = document.
            CreateElement (Nodename[0]);
            XmlNode parentnode = root; Document.
            AppendChild (root);
                    if (Nodename.count > 0) {for (int m = 1; m < nodename.count-1; m++) { XmlNode xnode = document.
                    CreateNode (XmlNodeType.Element, nodename[m], NULL);
     Parentnode.appendchild (XNode);               ParentNode = XNode;
                }//Set property value foreach (DictionaryEntry de in HT) { XmlNode xnode = document.
                CreateNode (XmlNodeType.Element, nodename[nodename.count-1], NULL);
                Parentnode.appendchild (XNode); XmlNode attr = document.
                CreateNode (Xmlnodetype.attribute, "name", null); attr. Value = de.
                Key.tostring ();
                XNode.Attributes.SetNamedItem (attr); XmlNode Attr_value = document.
                CreateNode (Xmlnodetype.attribute, "value", null); Attr_value. Value = de.
                Value.tostring ();
                XNode.Attributes.SetNamedItem (Attr_value);
            Xnode.attributes. } document.
            
        Save (Application.startuppath + "\" + fileName); }
 invocation mode: 
 list<string> List = new list<string> ()             & nbsp   list.
ADD ("config");                 list.
ADD ("Deviconfig");                 list.


ADD ("property");
                Hashtable ht = new Hashtable ();                 HT.
ADD ("Device", "SP-3000");                 HT.
ADD ("PortName", "COM1");                 HT.
ADD ("BaudRate", "19200");                 HT.
ADD ("DataBits", "8");                 HT.
ADD ("StopBits", "0");                 HT.
ADD ("Parity", "0");                 HT.
ADD ("Handshake", 1); 
Xmlfileutils.createxmlfile ("Config\\imagedevices\\series.xml", List, HT);
Finally, the XML file structure is generated as follows:
<?xml version= "1.0" encoding= "Utf-8"?>
<config>
<deviconfig>
<property name= "  Sourceindex "value=" 3 "/>
<property name=" Brightness "value=" 134 "/> <property name=" contrast "
value= "132"/> <property name= "imagetype" value=
"JPG"/> <property "name= imagefun" value= "
page"     />
<property name= "ImgWidth" value= "900"/> <property name=
"imghigth" value= "450"/>
<property name= "Toplength" value= "5"/> <property name=
"Leftlength" value= "5"/>
<pro Perty name= "ShortcutKey" value= "5"/>
</deviconfig>
</config>
 Note: The above tool class is used only to generate XML files of this type of data structure, for <node>value<node> The XML file for the struct type is not valid (Web search is basically the way to create this type of 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.