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);
                }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章