zoukankan      html  css  js  c++  java
  • C#的XML读取、增加、修改和删除操作

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

  • 相关阅读:
    《Real Time 3D Terrain Engines Using C++ And DirectX9》(实时地形引擎)随书源码
    (转)ogreUML类图
    (转)导弹跟踪算法
    转:正则表达式语法
    读取数据
    Python lambda用法及其与def的区别
    转Vb6.0安装失败解决办法,完全卸载VB(提高班里win7没装上VB的可以看看,我实验成功了)
    vb imagelist 作用
    二叉树的实现
    转:Python语言编程学习资料(电子书+视频教程)下载汇总:
  • 原文地址:https://www.cnblogs.com/sky7034/p/2112415.html
Copyright © 2011-2022 走看看