ASP. XML read, add, modify, and delete operations

Source: Internet
Author: User
C#-xml read, add, modify, and delete operations
The 1.xml file format is as follows:
<?xml version= "1.0" encoding= "Utf-8"?>
<projects>
<project name= "Platformframewo" vss-path= "Platform$/source Codhdt$pla~1.sln"/>
</projects>

1. Read
DataSet ds = new DataSet ();
Ds. READXML (Server.MapPath (@ "Projects.xml"));
DataTable dt = ds. Tables[0];
return DT;
The resulting datable in front of the loop output, omitted ...
<tr style= "Font-weight:bold;" >//Text Bold
<TD style= "Border-bottom:solid 2px Gray;" >//Text bottom plus Horizontal

2. New
XmlDocument xmldoc = new XmlDocument ();
String Path = Server.MapPath (@ "Projects.xml");
Xmldoc.load (Path);
XmlNode Root=xmldoc.selectsinglenode ("projects");
XmlElement xe1 = xmldoc.createelement ("project");
Xe1. SetAttribute ("name", Txtprojectname.text);
Strvsspath = Txtprojectvss.text + "$" + Txtprojectpath.text + "$" + txtprojectsln.text;
Xe1. SetAttribute ("Vss-path", Strvsspath);
Root. AppendChild (XE1);
Xmldoc.save (Path);

3. Modify
XmlDocument xmldoc = new XmlDocument ();
String Path = Server.MapPath (@ "Projects.xml");
Xmldoc.load (Path);
XmlNodeList nodelist = Xmldoc.selectsinglenode ("Projects"). ChildNodes;
foreach (XmlNode xn in nodelist)
{
XmlElement xe = (XmlElement) xn;
if (XE). GetAttribute ("name") = = = request["Name"]. ToString ())
{
Xe. SetAttribute ("name", Txtprojectname1.text);
Strvsspath = Txtprojectvss1.text + "$" + Txtprojectpath1.text + "$" + txtprojectsln1.text;
XE. SetAttribute ("Vss-path", Strvsspath);
Xmldoc.save (Path);
}
}

4. Delete
XmlDocument xmldoc = new XmlDocument ();
String Path = Server.MapPath (@ "Projects.xml");
Xmldoc.load (Path);
XmlNodeList nodelist = Xmldoc.selectsinglenode ("Projects"). ChildNodes;
foreach (XmlNode xn in nodelist)
{
XmlElement XE = (XmlElement) xn;
if (XE. GetAttribute ("name") = = = request["Name"]. ToString ())
{
Xn. Parentnode.removechild (xn);
Xmldoc.save (Path);
}
}

  • 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.