C#对xml的基本操作

时间:2021-10-15 05:43:35

代码原型为一个投票系统保存投票数的一个功能.

先建一个简单的xml文件:

<?xml version="1.0" encoding="utf-8"?>
<vote>
  <answer type="0">
    <num>12</num>
  </answer>
  <answer type="1">
    <num>8</num>
  </answer>
  <answer type="2">
    <num>18</num>
  </answer>
</vote>

最基础的C#代码(写入数据):

 1 string xmlFilePath = Server.MapPath("/Html/Activity/006/file/data.xml");
 2             XmlDocument xmlDoc = new XmlDocument();
 3             xmlDoc.Load(xmlFilePath);
 4             XmlNodeList nodeList = xmlDoc.SelectSingleNode("vote").ChildNodes; //找到父节点为vote下的所有子节点
 5             foreach (XmlNode xn in nodeList)
 6             {
 7                 XmlElement xe = (XmlElement)xn;
 8                 if (xe.GetAttribute("type") == content) //找到子节点type相对应值下的所有子节点
 9                 {
10                     XmlNodeList nls = xe.ChildNodes;
11                     foreach (XmlNode xn1 in nls)
12                     {
13                         XmlElement xe2 = (XmlElement)xn1;
14                         xe2.InnerText = (Convert.ToInt32(xe2.InnerText) + 1).ToString();  //目标子节点的值进行+1的操作
15                     }
16                 }
17             }
18             xmlDoc.Save(xmlFilePath);

//比较简单,只需要知道如何读取一个xml文档,然后写入数据,最后保存xml.

C#最简单的读取数据:

        List<string> list = new List<string>();
       string xmlFilePath = Server.MapPath("/Html/Activity/006/file/data.xml");
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(xmlFilePath);
            XmlNodeList nodeList = xmlDoc.SelectSingleNode("vote").ChildNodes;
            foreach (XmlNode xn in nodeList)
            {
                XmlElement xe = (XmlElement)xn;
                foreach (XmlNode xn1 in xe.ChildNodes)
                {
                    XmlElement xe2 = (XmlElement)xn1;
                    list.Add(xe2.InnerText);
                }
            }
//首先申明一个List,然后将xml读取到的数据放到list里面,再然后,你懂的.

 

加强版:

引入xml.linq

改进上面的xml

<?xml version="1.0" encoding="utf-8"?>
<vote>
  <answer type="0" num="6" />
  <answer type="1" num="2" />
  <answer type="2" num="3" />
</vote>

C#写入数据:

       var p = CustomUtility.GetMapPath("/Html/Activity/006/file/data.xml");
            var root = XElement.Load(p);
            var answer = root.Elements("answer").Where(a => a.Attribute("type").Value == content).FirstOrDefault();
            if (answer != null)
            {
                var n = Int64.Parse(answer.Attribute("num").Value);
                answer.SetAttributeValue("num", n + 1);
            }
            else
            {
                root.Add(new XElement("answer", new XAttribute("type", content), new XAttribute("num", 1))); //如果xml没有对应的数据,将会新建相对应的数据
            }
            root.Save(p);

 

写入数据:

1 List<string> list = new List<string>();
2             var p = CustomUtility.GetMapPath(act_006_xmlPath);
3             var root = XElement.Load(p);
4             root.Elements("answer").ToList().ForEach(s => {
5                 list.Add(s.Attribute("num").Value);
6             });