XML的操作

感觉几个世纪没有来博客园记录了

荒废了好久的时间,还是得把写过的东西记录下来才行,今天记录下对XML的操作。

修改XML
 
  XmlDocument  local = new XmlDocument();
  local.Load(path+"File.xml");
//获得XML中某一项的值
  XmlNode initSet = local.SelectSingleNode(@"xml/initSet");
  string  value=initSet.Attributes["value"].Value

   修改XML

 //在修改XML之前如果没有对XML进行过修改,则可以不用重新加载一次
 //不然XML取的是第一次加载XML时的内容
           local.Load(path + “File.xml”);
           XmlNode xmlNode = local.SelectSingleNode(@“xml/initSet”);
//转换成XmlElement类型
            ((XmlElement)xmlNode).SetAttribute("value", value);
            local.Save(path +  “File.xml”);//更新要开始时,更新为false  

添加XML

 //获取根  
XmlNode node = xml.SelectSingleNode(@"xml/files");
                    XmlNode x = xml.CreateElement("item");
                    ((XmlElement)x).SetAttribute("name", item.Name);
                    ((XmlElement)x).SetAttribute("version", item.Version);
                    ((XmlElement)x).SetAttribute("updateData", item.UpdateData);
                    ((XmlElement)x).SetAttribute("size", item.Size);
                    ((XmlElement)x).SetAttribute("filepath", item.Filepath);                   
                    ((XmlElement)x).SetAttribute("keyvalue", item.Keyvalue);
                    ((XmlElement)x).SetAttribute("isRegister", item.IsRegister);
//在根下添加子项
                    node.AppendChild(x);
  //依然,在没有改变过XML的情况下不需要重新LOAD XML 
                    xml.Save(localUrl + "File.xml");

移除XML

//找到类型XmlNode 某个节点后移除 
node.RemoveChild(XmlNode node);
原文地址:https://www.cnblogs.com/pigddyou/p/xml.html