Ways to add XML records

Source: Internet
Author: User
Tags add array insert relative string
Xml Write the code today, add a few XML records, the total feeling of writing that several repeated statements very uncomfortable, wrote this method

The code is as follows:

/**//* Add XML method by Lone Hung *
private void Addxmlinfo (string xmlpath,string rootelement,string rootpoint,int elementnum,string[,] addinfo)
... {
XmlDocument xdt = new XmlDocument ();
Xdt. Load (Server.MapPath (Xmlpath));
XmlNode root = xdt. selectSingleNode (rootelement);
XmlElement rootly = xdt. CreateElement (Rootpoint);
for (int i=0;i<elementnum;i++)
... {
xmlelement[] temp = new Xmlelement[elementnum];
temp = xdt. CreateElement (addinfo[i,0]);
Temp. innertext = addinfo[i,1];
rootly. AppendChild (temp);
}
Root. AppendChild (rootly);
Xdt. Save (Server.MapPath (Xmlpath));
}

method has 5 parameters, the following 5 parameters are described below:

Parameter OneXmlpath,String Type, representing the relative path of the XML document
Parameter tworootelement,String TypeThat represents the root element of an XML document
Parameter threeRootpoint,String TypeThat represents the root element of each record in an XML document
Parameter FourElementnum,int type, representing the number of child elements per record
Parameter FiveAddinfo,Array TypeThat represents a two-dimensional array that stores information for each record, including the element name and the value of the element

Here's an example:

The XML document structure used to demonstrate is as follows:
<?xml version= "1.0" encoding= "Utf-8"?>
<ProgramboyGuestbook>
<ly>
<Gcontent> This is an XML document that is used to illustrate the method of inserting XML records, and my relative path is data/test.xml</gcontent>
<Gname> Lone Hung </Gname>
<gtime>2006-9-18 8:49:23</gtime>
<Gweb>http://www.programboy.com</Gweb>
</ly>
</ProgramboyGuestbook>

Here we call the Insert method, which implements the insert operation:

This.addxmlinfo ("Data/test.xml", "Programboyguestbook", "ly", 4,new string[,] {{"gcontent", thecontent},{"Gname", thename},{"Gtime", thetime},{"Gweb", theweb});



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.