ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

C#Linq to XML的简单读写

2021-01-16 10:01:57  阅读:215  来源: 互联网

标签:XML string item C# Linq XElement XAttribute itemValue new


  • 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);
                }

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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有