位置:首頁 > 軟件操作教程 > 編程開發(fā) > C# > 問題詳情

C# 處理XML片段

提問人:劉團圓發(fā)布時間:2020-12-10

    與一些XML DOM不同,LINQ to XML處理XML片段(部分或不完整的XML文檔)的方式與處理完整的XML文檔幾乎完全相同。在處理片段時,只需要將XElement(而不是XDocument)當作頂級XML對象。


下面的示例會加載、保存和處理XML元素及其子節(jié)點,這與處理XML文檔一樣。

試一試  處理 XML 片段:BeginningCSharp7—22_2—XMLFragments

按照下面的步驟在Visual Studio 2017中創(chuàng)建示例:

⑴在C:\BegirmingCSharp7\Chapter22目錄中修改上一個示例或者創(chuàng)建一個新的控制臺應用程序 BeginningCSharp7 一 22—2 一 XMLFragments。

(2)打開主源文件Program.cs。

(3)在Program.cs幵頭處添加對System.XmLLinq名稱空間的引用,如下所示:

using System;

using System.Collections.Generic; 

using System.Xml.Linq; 

using System.Text; 

using static System,Console;

如果正在修改上一個示例,則己經(jīng)引用了這個名稱空間。

(4)把上一個示例中的XML元素(不包含,XML文檔構(gòu)造函數(shù))添加到Program.cs的Main()方法中:

static void Main(string[] args)

{

    XElement xcust =

        new XElement("customers", 

            new XElement("customer",

                new XAttribute("ID", "A"),

                new XAttribute("City", "New York"}, 

                new XAttribute("Region", "North America") 

                new XElement("order",

                    new XAttribute("Item", "Widget")

                    new XAttribute("Price", 100)

                ),

                new XElement ("order",

                    new XAttribute("Item", "Tire"),

                    new XAttribute("Price", 200)

                )

            ),

            new XElement(ncustomer",

                new XAttribute("ID", "B"), 

                new XAttribute("City", "Mumbai"), 

                new X&ttribute("Region", "Asia"), 

                new XEloment ("order",

                    new XAttribute("Item", "Oven"), 

                    new XAttribute("Price", 501)

                )

            )

        )

;

(5)在上一步添加了XML元素構(gòu)造函數(shù)代碼后,添加下面的代碼,以便保存、加載和顯示XML元素:

        string xmlFileName =

@"c:\BeginningCSharp7\Chapter22\BeginningCSharp7_22_2_XMLFragments\fragment.xml";

        xcust.Save(xmlFileName);

        XElement xcust2 = XElement.Load(xmlFileName);

        WriteLine("Contents of xcust:");

        WriteLine(xcust);

        Write("Program finished, press Enter/Return to continue:");

        ReadLine();

(6)編譯并執(zhí)行程序(按下F5鍵即可啟動調(diào)試),控制臺窗口中的輸出結(jié)果如下所示:

Contents of XElement xcust2:

<customers>

  <customer ID="A" City="New York" Region="North America"〉

    <order Item="Wiciget" Price="100" />

    <order Item="Tire" Price="200" />

  </customer>

  <customer ID="B" City="Mumbai" Region="Asia">

    <order Item="0ven" Price="501" />

  </customer>

</customers>

Program finished, press Enter/Return to continue:

    按下Enter/Retum鍵,以便結(jié)束程序,關(guān)閉控制臺屏幕。如果使用Ctrl+F5組合鍵(啟動時不使用調(diào)試功能),就需要按下Enter/Retum鍵兩次。


示例說明

    XElement和XDocument都繼承自LINQ to XML類XContainer,它實現(xiàn)了 一個可以包含其他XML節(jié)點的XML節(jié)點。這兩個類都實現(xiàn)了Iuad()和Save()方法,因此,可在LINQ to XML的XDocument上執(zhí)行的大多數(shù)操作都可以在XElement實例及其子元素上執(zhí)行。

    這里只創(chuàng)建了一個XElement實例,它的結(jié)構(gòu)與前面示例中的XDocument相同,但不包含XDocument。這個程序的所有操作處理的都是XElement片段。

    XElement還支持LoadO和Parse()方法,可以分別從文件和字符串中加載XML。

繼續(xù)查找其他問題的答案?

相關(guān)視頻回答
回復(0)
返回頂部