编程语言
首页 > 编程语言> > C#Linq to XML的简单读写

C#Linq to XML的简单读写

作者:互联网

                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);//保存
           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);
                }

标签:XML,string,item,C#,Linq,XElement,XAttribute,itemValue,new
来源: https://blog.csdn.net/qq_44305000/article/details/112691846