zoukankan      html  css  js  c++  java
  • C#将XML数据保存读取删除

                    //创建一个数据集,将其写入xml文件
                    string name = "1.xml";
                    System.Data.DataSet ds = new System.Data.DataSet("MESSAGE");
                    System.Data.DataTable table = new System.Data.DataTable("FeedBack");
                    ds.Tables.Add(table);
                    table.Columns.Add("Model_Name", typeof(string));
                    table.Columns.Add("PRJ_Name", typeof(string));
                    table.Columns.Add("area_name", typeof(string));
                    table.Columns.Add("Major_Name", typeof(string));
                    System.Data.DataRow row = table.NewRow();
                    row[0] = Model_Name;
                    row[1] = PRJ_Name;
                    row[2] = area_name;
                    row[3] = Major_Name;
                    ds.Tables["FeedBack"].Rows.Add(row);
                    string path = ("E:/BIM_APP/BIM_APP_ModelInsp/" + name);
                    ds.WriteXml(path);

    这个方法只是针对临时存放的数据,多次向XML里面添加数据只会保存最后一次添加的数据,不是全部保存。

    XML展示

    <?xml version="1.0" standalone="yes"?>
    <MESSAGE>
      <FeedBack>
        <Model_Name>name</Model_Name>
        <PRJ_Name>test</PRJ_Name>
        <area_name>test</area_name>
        <Major_Name>test</Major_Name>
      </FeedBack>
    </MESSAGE>

    读取XML数据

     XmlDocument doc = new XmlDocument();
                doc.Load("E:/BIM_APP/BIM_APP_ModelInsp/1.xml");
                XmlElement xmlRoot = doc.DocumentElement;
                foreach (XmlNode node in xmlRoot.ChildNodes)
                {
                    label21.Text = node["Model_Name"].InnerText;
                    label23.Text = node["PRJ_Name"].InnerText;
                    label25.Text = node["area_name"].InnerText;
                    label26.Text = node["Major_Name"].InnerText;
                }

    删除方法

                XmlDocument xdoc = new XmlDocument();
                xdoc.Load("E:/BIM_APP/BIM_APP_ModelInsp/1.xml");
                //获得元素列表
                XmlElement xeXML = xdoc.DocumentElement;
                //获得父节点数量
                int nodeCount = xeXML.ChildNodes.Count;
                for (int i = 0; i < nodeCount; i++)
                {
                    XmlNode root = xdoc.SelectSingleNode("MESSAGE");
                    root.RemoveChild(xeXML.ChildNodes[i]);
                    nodeCount = nodeCount - 1;
                    xdoc.Save("E:/BIM_APP/BIM_APP_ModelInsp/1.xml");
                }
                nodeCount = nodeCount - 1;

    这种删除方法建议用在删除全部的数据上,MESSAGE就是XML的节点,删除这个节点下面全部的数据。

    读取到指定的节点

      XmlDocument xml = new XmlDocument();
                xml.Load(strUrl);
             
                var selectItemList = new List<Translation>();
                XDocument xdoc = XDocument.Load(strUrl);
                XElement xroot = xdoc.Root;//根节点
                var nodes = xroot.Descendants().FirstOrDefault(a => a.Name.LocalName == Nodes);//获取指定的XML节点
    
             
                foreach (XElement e in nodes.Elements("Param"))
                {
                    selectItemList.Add(new Translation() { Text = e.Value, Value = e.FirstAttribute.Value, Name = e.LastAttribute.Value });
                }
    

      

      

  • 相关阅读:
    利用Java脚本实现弹出窗口后,按确定实现跳转
    客服利用QQ实现即时聊天
    获取页面可见区域,屏幕区域的尺寸
    圆角模板百度知道
    利用javascript实现web页面刷新的方法
    论:命名空间,程序集和类
    我从少年时候就非常喜欢的诗歌:雨巷
    魔兽世界 圣骑士唯一的远程武器任务
    又想起我年少时候熟记的抒情诗致海伦
    System.Text.Encoding.UTF8 字符串和字节数组的互相转换
  • 原文地址:https://www.cnblogs.com/Angdybo/p/7943127.html
Copyright © 2011-2022 走看看