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});