LINQ to XML編程(一)

1、LINQ to XML
System.Xml.Linq命名空間含有19個類,下表列出了它們的名稱及其描述:
描述
XAttribute
表示一個 XML 屬性
XCData
表示一個 CDATA 文本節點
XComment
表示一個 XML 註釋
XContainer
適用於可能具有子節點的所有節點的抽象基類
XDeclaration
表示一個 XML 聲明
XDocument
表示一個 XML 文檔
XDocumentType
表示一個 XML 文檔類型定義 (DTD)
XElement
表示一個 XML 元素
XName
表示一個XML元素或屬性的名稱
XNamespace
表示一個XML的命名空間
XNode
一個抽象類,它表示 XML 樹的節點
XNodeDocumentOrderComparer
提供用於比較節點的文檔順序的功能
XNodeEqualityComparer
提供用於比較節點的值是否相等的功能
XObject
XNode 和 XAttribute 的抽象基類
XObjectChange
XObject引發事件時的事件類型
XObjectChangeEventArgs
爲 Changing 和 Changed 事件提供數據
XProcessingInstruction
表示一個 XML 處理指令
XText
表示一個文本節點
以下的代碼演示瞭如何使用LINQ to XML來快速創建一個xml:
public static void CreateDocument()
{
    XDocument xdoc = new XDocument
    (
        new XDeclaration("1.0", "utf-8", "yes"),
        new XElement("Root","root")
    );
    xdoc.Save(path);
}
運行該示例將會得到一個xml文件,其內容爲:
<?xmlversion="1.0"encoding="utf-8"standalone="yes"?>
<Root>root</Root>
可以看出微軟在LINQ上投入了很大的精力,使我們在編程時感覺到很舒服。下面將詳細介紹處理XML時使用最多的三個類:XElement、XAttribute和XDocument。如果掌握了這些類,使用LINQ to XML時將會感到很順手。
2、XElement
XElement 類是 LINQ to XML 中的基礎類之一。 它表示一個 XML 元素。 可以使用該類創建元素;更改元素內容;添加、更改或刪除子元素;向元素中添加屬性;或以文本格式序列化元素內容。 還可以與 System.Xml 中的其他類(例如 XmlReader、XmlWriter 和 XslCompiledTransform)進行互操作。
使用LINQ to XML創建xml文檔有很多種方式,具體使用哪種方法要根據實際需要。而創建xml文檔最簡單、最常見的方式是使用XElement類。以下的代碼演示瞭如何使用XElement類創建一個xml文檔:
public static void CreateCategories()
{
    XElement root = new XElement("Categories",
        new XElement("Category",
            new XElement("CategoryID", Guid.NewGuid()),
            new XElement("CategoryName", "Beverages")
            ),
        new XElement("Category",
            new XElement("CategoryID", Guid.NewGuid()),
           new XElement("CategoryName", "Condiments")
            ),
        new XElement("Category",
            new XElement("CategoryID", Guid.NewGuid()),
            new XElement("CategoryName", "Confections")
            )
       );
    root.Save(path);
}
運行該示例將會得到一個xml文件,其內容爲:
<?xmlversion="1.0"encoding="utf-8"?>
<Categories>
 <Category>
    <CategoryID>57485174-46fc-4e8c-8d98-d25b53d504a1</CategoryID>
    <CategoryName>Beverages</CategoryName>
 </Category>
 <Category>
    <CategoryID>1474dde1-8014-48f7-b093-b47ca5d5b770</CategoryID>
    <CategoryName>Condiments</CategoryName>
 </Category>
 <Category>
    <CategoryID>364224e0-e002-4939-90fc-0fd93e0cf35b</CategoryID>
    <CategoryName>Confections</CategoryName>
 </Category>
</Categories>
LINQ to XML的強大之處還在於它可以使用LINQ to SQL或者LINQ to Object獲取數據源,然後填充到xml樹。以下的示例從Northwind數據庫中讀取Categories、Products表中的數據來創建包含產品類別,以及每個類別下所有產品的xml文件:
public static void CreateCategoriesFromDatabase()
{
    using (NorthwindDataContext db = new NorthwindDataContext())
    {
        XElement root = new XElement("Categories",
                db.Categories
                .Select
                (
                    c => new XElement
                    (
                        "Category"
                        , new XElement("CategoryID", c.CategoryID)
                        , new XElement("CategoryName", c.CategoryName)
                        , new XElement
                            (
                                "Products"
                                , c.Products
                                .Select
                                (
                                    p => new XElement
                                    (
                                        "Product"
                                        , new XElement("ProductName", p.ProductName)
                                    )
                                )
                                .Take(2)
                            )
                    )
                )
                .Take(3)
            );
        root.Save(path);
    }
}
運行該示例將會得到一個xml文件,其內容爲:
<?xmlversion="1.0"encoding="utf-8"?>
<Categories>
 <Category>
    <CategoryID>1</CategoryID>
    <CategoryName>Beverages</CategoryName>
    <Products>
      <Product>
        <ProductName>Chai</ProductName>
      </Product>
      <Product>
        <ProductName>Chang</ProductName>
      </Product>
    </Products>
 </Category>
 <Category>
    <CategoryID>2</CategoryID>
    <CategoryName>Condiments</CategoryName>
    <Products>
      <Product>
        <ProductName>Aniseed Syrup</ProductName>
      </Product>
      <Product>
        <ProductName>Chef Anton's Cajun Seasoning</ProductName>
      </Product>
    </Products>
 </Category>
 <Category>
    <CategoryID>3</CategoryID>
    <CategoryName>Confections</CategoryName>
    <Products>
      <Product>
        <ProductName>Pavlova</ProductName>
      </Product>
      <Product>
        <ProductName>Teatime Chocolate Biscuits</ProductName>
      </Product>
    </Products>
 </Category>
</Categories>
XElement類包含了許多方法,這些方法使得處理xml變得輕而易舉。有關這些方法請參照MSDN。
其中,Save、CreateReader、ToString和WriteTo方法是比較常用的三個方法:
方法
參數
返回值
描述
CreateReader
System.Xml.XmlReader
創建此節點的 XmlReader
Save
System.String
void
將此元素序列化爲文件
System.IO.TextWriter
void
將此元素序列化爲 TextWriter
System.Xml.XmlWriter
void
將此元素序列化爲 XmlWriter
System.String,
System.Xml.Linq.SaveOptions
void
將此元素序列化爲文件,並可以選擇禁用格式設置
System.IO.TextWriter,
System.Xml.Linq.SaveOptions
void
將此元素序列化爲 TextWriter,並可以選擇禁用格式設置
WriteTo
System.Xml.XmlWriter
void
將此元素寫入 XmlWriter
ToString
System.String
返回此節點的縮進 XML
System.Xml.Linq.SaveOptions
System.String
返回此節點的 XML,並可以選擇禁用格式設置
現在有很多使用XmlReader作爲數據源的應用程序,使用XElement可以很方便地提供支持。
3、XAttribute
XAttribute類用來處理元素的屬性,屬性是與元素相關聯的“名稱-值”對,每個元素中不能有名稱重複的屬性。使用XAttribute類與使用XElement類的操作十分相似,下面的示例演示瞭如何在創建xml樹時爲其添加一個屬性:
public static XElement CreateCategoriesByXAttribute()
{
    XElement root = new XElement("Categories",
        new XElement("Category",
            new XAttribute("CategoryID", Guid.NewGuid()),
            new XElement("CategoryName", "Beverages")
            ),
        new XElement("Category",
            new XAttribute("CategoryID", Guid.NewGuid()),
            new XElement("CategoryName", "Condiments")
            ),
        new XElement("Category",
            new XAttribute("CategoryID", Guid.NewGuid()),
            new XElement("CategoryName", "Confections")
            )
       );
    root.Save(path);
    return root;
}
運行該示例將會得到一個xml文件,其內容爲:
<?xmlversion="1.0"encoding="utf-8"?>
<Categories>
 <CategoryCategoryID="a6d5ef04-3f83-4e00-aeaf-52444add7570">
    <CategoryName>Beverages</CategoryName>
 </Category>
 <CategoryCategoryID="67a168d5-6b22-4d82-9bd4-67bec88c2ccb">
    <CategoryName>Condiments</CategoryName>
 </Category>
 <CategoryCategoryID="17398f4e-5ef1-48da-8a72-1c54371b8e76">
    <CategoryName>Confections</CategoryName>
 </Category>
</Categories>
XAttribute類的方法比較少,常用的三個是:
方法
描述
AddAnnotation
爲該屬性添加註解
Remove
刪除該屬性
SetValue
設定該屬性的值
以下的示例使用Remove來刪除第一個元素的CategoryID屬性:
public static void RemoveAttribute()
{
    XElement xdoc = CreateCategoriesByXAttribute();
    XAttribute xattr = xdoc.Element("Category").Attribute("CategoryID");
    xattr.Remove();
    xdoc.Save(path);
}
運行該示例將會得到一個xml文件,其內容爲:
<?xmlversion="1.0"encoding="utf-8"?>
<Categories>
 <Category>
    <CategoryName>Beverages</CategoryName>
 </Category>
 <CategoryCategoryID="5c311c1e-ede5-41e5-93f7-5d8b1d7a0346">
    <CategoryName>Condiments</CategoryName>
 </Category>
 <CategoryCategoryID="bfde8db5-df84-4415-b297-cd04d8db9712">
    <CategoryName>Confections</CategoryName>
 </Category>
</Categories>
作爲嘗試,試一試以下刪除屬性的方法:
public static void RemoveAttributeByDoc()
{
    XElement xdoc = CreateCategoriesByXAttribute();
    XAttribute xattr = xdoc.Attribute("CategoryID");
    xattr.Remove();
    xdoc.Save(path);
}
運行該示例將會拋出一個空引用異常,因爲元素Categories沒有一個叫做CategoryID的屬性。
4、XDocument
XDocument類提供了處理xml文檔的方法,包括聲明、註釋和處理指令。一個XDocument對象可以包含以下內容:
對象
個數
說明
XDeclaration
一個
用於指定xml聲明中的重要組成部分,如文檔編碼和版本等
XElement
一個
指定文檔的根元素
XDocumentType
一個
表示一個xml DTD
XComment
多個
Xml註釋。它不能是第一個參數,因爲一個有效的xml文檔不能以註釋作爲開始
XProcessingInstruction
多個
爲處理xml的應用程序指定任何所需信息
下面的示例創建了一個簡單的xml文檔,它包含幾個元素和一個屬性,以及一個處理指令和一些註釋:
public static void CreateXDocument()
{
    XDocument xdoc = new XDocument(
            new XProcessingInstruction("xml-stylesheet", "title='EmpInfo'"),
            new XComment("some comments"),
            new XElement("Root",
                    new XElement("Employees",
                            new XElement("Employee",
                                    new XAttribute("id", "1"),
                                    new XElement("Name", "Scott Klein"),
                                    new XElement("Title", "Geek"),
                                    new XElement("HireDate", "02/05/2007"),
                                    new XElement("Gender", "M")
                                )
                        )
                ),
            new XComment("more comments")
        );
    xdoc.Save(path);
}
運行該示例將會得到一個xml文件,其內容爲:
<?xmlversion="1.0"encoding="utf-8"?>
<?xml-stylesheettitle='EmpInfo'?>
<!--some comments-->
<Root>
 <Employees>
    <Employeeid="1">
      <Name>Scott Klein</Name>
      <Title>Geek</Title>
      <HireDate>02/05/2007</HireDate>
      <Gender>M</Gender>
    </Employee>
 </Employees>
</Root>
<!--more comments-->
XDocument類包含多個與XElement類相同的方法,具體內容可以參閱MSDN。需要注意的是,處理節點和元素的大部分功能都可以通過XElement獲得,只有當絕對需要文檔層次的處理能力,以及需要訪問註釋、處理指令和聲明時,纔有使用XDocument類的必要。
創建了xml文檔後,可以使用NodesAfterSelf方法返回指定的XElement元素之後的所有同級元素。需要注意的是,此方法只包括返回集合中的同級元素,而不包括子代。此方法使用延遲執行。以下代碼演示了這一過程:
public static void NodesAfterSelf()
{
    XElement root = new XElement("Categories",
        new XElement("Category",
                new XElement("CategoryID", Guid.NewGuid()),
                new XElement("CategoryName", "食品"),
                new XElement("Description", "可以吃的東西")
            )
        );
    foreach (var item in root.Element("Category").Element("CategoryID").NodesAfterSelf())
    {
        Console.WriteLine((item as XElement).Value);
    }
}
使用LINQ to XML中的類來處理xml十分簡單和高效,包括創建、查詢和操縱xml。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章