ASP.NET XML讀取、增加、修改和刪除操作

來源:互聯網
上載者:User
C#—XML讀取、增加、修改和刪除操作
1.xml檔案格式如下:
<?xml version="1.0" encoding="utf-8"?>
<projects>
<project name="PlatformFramewo" vss-path="Platform$/Source CodHdt$Pla~1.sln" />
</projects>

1.讀取
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath(@"Projects.xml"));
DataTable dt = ds.Tables[0];
return dt;
//得到的datable在前台進行迴圈輸出,省略...
<tr style="font-weight: bold;"> //文字加粗
<td style="border-bottom: solid 2px gray;"> //文字底部加橫線

2.新增
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.修改
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.刪除
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);
}
}

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.