XElement 類別概觀 (C#)XElement Class Overview (C#)

XElement 類別是 LINQ to XMLLINQ to XML 中的其中一個基本類別。The XElement class is one of the fundamental classes in LINQ to XMLLINQ to XML. 它代表 XML 項目。It represents an XML element. 您可以使用這個類別來建立項目;變更項目的內容;加入、變更或刪除子項目;將屬性加入到項目中;或以文字格式序列化項目的內容。You can use this class to create elements; change the content of the element; add, change, or delete child elements; add attributes to an element; or serialize the contents of an element in text form. 您也可以與 System.Xml 中的其他類別相互溝通,例如,XmlReaderXmlWriterXslCompiledTransformYou can also interoperate with other classes in System.Xml, such as XmlReader, XmlWriter, and XslCompiledTransform.

本主題描述 XElement 類別提供的功能。This topic describes the functionality provided by the XElement class.

建構 XML 樹狀結構Constructing XML Trees

您可以用各種方式建構 XML 樹狀結構,包括:You can construct XML trees in a variety of ways, including the following:

  • 您可以在程式碼中建構 XML 樹狀結構。You can construct an XML tree in code. 如需詳細資訊,請參閱建立 XML 樹狀結構 (C#)For more information, see Creating XML Trees (C#).

  • 您可以剖析來自各種來源的 XML,包括 TextReader、文字檔或網路位址 (URL)。You can parse XML from various sources, including a TextReader, text files, or a Web address (URL). 如需詳細資訊,請參閱剖析 XML (C#)For more information, see Parsing XML (C#).

  • 您可以使用 XmlReader 來填入樹狀結構。You can use an XmlReader to populate the tree. 如需詳細資訊,請參閱 ReadFromFor more information, see ReadFrom.

  • 如果您擁有的模組可以將內容寫入到 XmlWriter,您可以使用 CreateWriter 方法來建立寫入器、將寫入器傳遞到模組,然後使用寫入到 XmlWriter 的內容填入 XML 樹狀結構。If you have a module that can write content to an XmlWriter, you can use the CreateWriter method to create a writer, pass the writer to the module, and then use the content that is written to the XmlWriter to populate the XML tree.

不過,建立 XML 樹狀結構最常見的方式為下:However, the most common way to create an XML tree is as follows:

XElement contacts =  
    new XElement("Contacts",  
        new XElement("Contact",  
            new XElement("Name", "Patrick Hines"),
            new XElement("Phone", "206-555-0144"),  
            new XElement("Address",  
                new XElement("Street1", "123 Main St"),  
                new XElement("City", "Mercer Island"),  
                new XElement("State", "WA"),  
                new XElement("Postal", "68042")  
            )  
        )  
    );  

建立 XML 樹狀結構的另一個非常常見的技術,包括使用 LINQ 查詢的結果來填入 XML 樹狀結構,如下列範例所示:Another very common technique for creating an XML tree involves using the results of a LINQ query to populate an XML tree, as shown in the following example:

XElement srcTree = new XElement("Root",  
    new XElement("Element", 1),  
    new XElement("Element", 2),  
    new XElement("Element", 3),  
    new XElement("Element", 4),  
    new XElement("Element", 5)  
);  
XElement xmlTree = new XElement("Root",  
    new XElement("Child", 1),  
    new XElement("Child", 2),  
    from el in srcTree.Elements()  
    where (int)el > 2  
    select el  
);  
Console.WriteLine(xmlTree);  

這個範例會產生下列輸出:This example produces the following output:

<Root>  
  <Child>1</Child>  
  <Child>2</Child>  
  <Element>3</Element>  
  <Element>4</Element>  
  <Element>5</Element>  
</Root>  

序列化 XML 樹狀結構Serializing XML Trees

您可以將 XML 樹狀結構序列化至 FileTextWriterXmlWriterYou can serialize the XML tree to a File, a TextWriter, or an XmlWriter.

如需詳細資訊,請參閱序列化 XML 樹狀結構 (C#)For more information, see Serializing XML Trees (C#).

透過座標軸方法擷取 XML 資料Retrieving XML Data via Axis Methods

您可以使用座標軸方法來擷取屬性、子項目、子系項目以及祖系項目。You can use axis methods to retrieve attributes, child elements, descendant elements, and ancestor elements. LINQ 查詢會在座標軸方法上運作,並提供數個彈性且功能強大的方式來流覽和處理 XML 樹狀結構。LINQ queries operate on axis methods, and provide several flexible and powerful ways to navigate through and process an XML tree.

如需詳細資訊,請參閱 LINQ to XML 座標軸 (C#)For more information, see LINQ to XML Axes (C#).

查詢 XML 樹狀Querying XML Trees

您可以撰寫從 XML 樹狀結構中解壓縮資料的 LINQ 查詢。You can write LINQ queries that extract data from an XML tree.

如需詳細資訊,請參閱查詢 XML 樹狀結構 (C#)For more information, see Querying XML Trees (C#).

修改 XML 樹狀Modifying XML Trees

您可以用各種方式修改項目,包括變更其內容或屬性。You can modify an element in a variety of ways, including changing its content or attributes. 您也可以從其父代移除項目。You can also remove an element from its parent.

如需詳細資訊,請參閱修改 XML 樹狀結構 (LINQ to XML) (C#)For more information, see Modifying XML Trees (LINQ to XML) (C#).

另請參閱See also