C#Linq to XML的简单读写

  • Linq to XML
    Linq是C#3.0中出现的一个新特性,可以很方便操作XML文件

  • 写入数据
    需要引入using System.Xml.Linq;命名空间

                string dirPath =  "xmlData.xml";             
                XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance";
                XNamespace xsd = "http://www.w3.org/2001/XMLSchema";
                string Code1 = "A";       //属性1
                string Code2 = "B";      //属性2
                string Code3 = "C";        //属性3

                XDocument doc = new XDocument(
                    new XDeclaration("1.0", "UTF-8", null),//(版本,编码,独立属性)
                    new XElement("ROOT",     //添加根节点       
                        new XAttribute(XNamespace.Xmlns + "xsi", xsi),//添加命名空间
                        new XAttribute(XNamespace.Xmlns + "xsd", xsd),//添加命名空间
                        new XAttribute("Code1", Code1),//属性      
                        new XAttribute("Code2", Code2),//属性     
                        new XAttribute("Code3", Code3)//属性      
                        ));

                XElement rootS = doc.Root; //获取根元素

                XElement projects = new XElement("projects");//创建节点
                rootS.Add(projects);//将节点添加到根节点

                XElement project = new XElement("project");//创建节点
                projects.Add(project);//将节点project添加到节点projects
                string data1 = "AA";
                string data2 = "BB";
                project.Add(new XElement("item", new XAttribute("itemCode", data1), new XAttribute("itemValue", data2)),
                            new XElement("item", new XAttribute("itemCode", data1), new XAttribute("itemValue", data2)),
                            new XElement("item", new XAttribute("itemCode", data1), new XAttribute("itemValue", data2)),
                            new XElement("item", new XAttribute("itemCode", data1), new XAttribute("itemValue", data2)),
                            new XElement("item", new XAttribute("itemCode", data1), new XAttribute("itemValue", data2)),
                            new XElement("item", new XAttribute("itemCode", data1), new XAttribute("itemValue", data2)),
                            new XElement("item", new XAttribute("itemCode", data1), new XAttribute("itemValue", data2))
                            );

                doc.Save(dirPath);//保存
  • 读取数据
    如果想要查看xml文件的全部内容,可以通过读取文本的形式来读取xml文件内容
           string text = System.IO.File.ReadAllText("xmlData.xml");

xml文件内容:

<?xml version="1.0" encoding="utf-8"?>
<ROOT xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" Code1="A" Code2="B" Code3="C">
  <projects>
    <project>
      <item itemCode="AA" itemValue="BB" />
      <item itemCode="AA" itemValue="BB" />
      <item itemCode="AA" itemValue="BB" />
      <item itemCode="AA" itemValue="BB" />
      <item itemCode="AA" itemValue="BB" />
      <item itemCode="AA" itemValue="BB" />
      <item itemCode="AA" itemValue="BB" />
    </project>
  </projects>
</ROOT>

读取属性的值

                string path = "xmlData.xml";
                XDocument xdoc = XDocument.Load(path); //加载xml文件
                XElement rootS = xdoc.Root; //获取根元素
                string Code1 = rootS.Attribute("Code1").Value.ToString();//属性1的值
                string Code2 = rootS.Attribute("Code2").Value.ToString();//属性2的值
                string Code3 = rootS.Attribute("Code3").Value.ToString();//属性3的值
                 IEnumerable<XElement> projectitem = xdoc.Descendants("item");//定位到节点 
                List<string> ProjectList = new List<string>();
                foreach (var xElement in projectitem)//遍历节点获取节点的属性的值
                {
   
   
                    ProjectList.Add(xElement.Attribute("itemCode").Value);
                    ProjectList.Add(xElement.Attribute("itemValue").Value);
                }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章