Asp.net Method for writing and reading xml, asp.net for reading xml

Source: Internet
Author: User

Asp.net Method for writing and reading xml, asp.net for reading xml

Add namespace

Using System. Xml;

My own code (add nodes)

XmlDocument xmlDoc = new XmlDocument ();
XmlDoc. Load (Server. MapPath ("userTable. xml "));
XmlNode root = xmlDoc. SelectSingleNode ("root"); // query <root>
XmlElement xe1 = xmlDoc. CreateElement ("user"); // create a <user> node
// Xe1.SetAttribute ("genre", "Zhang San"); // you can specify the genre attribute of the node.
// Xe1.SetAttribute ("ISBN", "1-1111-1"); // set the ISBN attribute of the node

XmlElement xesub1 = xmlDoc. CreateElement ("ID ");
Xesub1.InnerText = "" + guid + ""; // set a text node
Xe1.AppendChild (xesub1); // Add it to the <user> node
XmlElement xesub2 = xmlDoc. CreateElement ("yhmc ");
Xesub2.InnerText = "" + username + "";
Xe1.AppendChild (xesub2 );
XmlElement xesub3 = xmlDoc. CreateElement ("tableRealationName ");
Xesub3.InnerText = "" + tableRealation_name + "";
Xe1.AppendChild (xesub3 );
// XmlElement xesub4 = xmlDoc. CreateElement ("imgDescribName ");
// Xesub4.InnerText = "" + imgDescrib_name + "";
// Xe1.AppendChild (xesub4 );
// XmlElement xesub5 = xmlDoc. CreateElement ("imgDescribName ");
// Xesub5.InnerText = "1450 ";
// Xe1.AppendChild (xesub5 );
Root. AppendChild (xe1); // Add it to the <root> node.
XmlDoc. Save (Server. MapPath ("userTable. xml "));

Effect

<? Xml version = "1.0" encoding = "UTF-8"?>
<Root>
<User genre = "zhangsan" ISBN = "1-1111-1">
<ID> guid </ID>
<Yhmc> yhmc </yhmc>
<TableRealationName> tableRealationName </tableRealationName>
</User>
</Root>

Modify the node (Attributes and subnodes)

XmlDocument xmlDoc = new XmlDocument ();
XmlDoc. Load (Server. MapPath ("userTable. xml "));

XmlNodeList nodeList = xmlDoc. SelectSingleNode ("Employees"). ChildNodes; // obtain all child nodes of the Employees Node

Foreach (XmlNode xn in nodeList) // traverses all subnodes
{
XmlElement xe = (XmlElement) xn; // converts the subnode type to the XmlElement type
If (xe. GetAttribute ("genre") = "Zhang San") // if the genre attribute value is "Zhang San"
{
Xe. SetAttribute ("genre", "update Zhang San"); // modify the attribute to "update Zhang San"

XmlNodeList nls = xe. ChildNodes; // continue to obtain all the child nodes of the xe subnode
Foreach (XmlNode xn1 in nls) // traverse
{
XmlElement xe2 = (XmlElement) xn1; // Conversion Type
If (xe2.Name = "author") // if you find
{
Xe2.InnerText = "ID"; // modify
}
}
}
}
XmlDoc. Save (Server. MapPath ("userTable. xml"); // Save.

Modify a node (adding node attributes and adding a node's self-node ):

XmlDocument xmlDoc = new XmlDocument ();
XmlDoc. Load (Server. MapPath ("userTable. xml "));

XmlNodeList nodeList = xmlDoc. SelectSingleNode ("Employees"). ChildNodes; // obtain all child nodes of the Employees Node

Foreach (XmlNode xn in nodeList)
{
XmlElement xe = (XmlElement) xn;
Xe. SetAttribute ("test", "111111 ");

XmlElement xesub = xmlDoc. CreateElement ("flag ");
Xesub. InnerText = "1 ";
Xe. AppendChild (xesub );
}
XmlDoc. Save (Server. MapPath ("userTable. xml "));

Read Method

String path = Server. MapPath ("userTable. xml ");
XmlDocument xmldoc = new XmlDocument ();
Xmldoc. Load (path );
// Obtain the specified subnode
XmlNode xn = xmldoc. SelectSingleNode ("root ");
XmlNodeList xnlist = xn. ChildNodes;
Foreach (XmlNode xn1 in xnlist)
{
XmlElement xe = (XmlElement) xn1;
XmlNodeList xnl0 = xe. ChildNodes;
String xmlynbh = xnl0.Item (0). InnerText;
If (xmlynbh = yhbh. ToString ())
{
TableRealationName = xnl0.Item (2). InnerText;
// ImgDescribName = xnl0.Item (3). InnerText;
}

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.