XDocument XDocument XDocument Class

Definition

Represents an XML document. For the components and usage of an XDocument object, see XDocument Class Overview.

public ref class XDocument : System::Xml::Linq::XContainer
public class XDocument : System.Xml.Linq.XContainer
Public Class XDocument
Inherits XContainer
Inheritance

Examples

The following example creates a document, and then adds a comment and an element to it. It then composes another document using the results of a query.

XDocument srcTree = new XDocument(  
    new XComment("This is a comment"),  
    new XElement("Root",  
        new XElement("Child1", "data1"),  
        new XElement("Child2", "data2"),  
        new XElement("Child3", "data3"),  
        new XElement("Child2", "data4"),  
        new XElement("Info5", "info5"),  
        new XElement("Info6", "info6"),  
        new XElement("Info7", "info7"),  
        new XElement("Info8", "info8")  
    )  
);  

XDocument doc = new XDocument(  
    new XComment("This is a comment"),  
    new XElement("Root",  
        from el in srcTree.Element("Root").Elements()  
        where ((string)el).StartsWith("data")  
        select el  
    )  
);  
Console.WriteLine(doc);  
Dim srcTree As XDocument = _   
    <?xml version="1.0" encoding="utf-8" standalone="yes"?>  
        <!--This is a comment-->  
        <Root>  
            <Child1>data1</Child1>  
            <Child2>data2</Child2>  
            <Child3>data3</Child3>  
            <Child2>data4</Child2>  
            <Info5>info5</Info5>  
            <Info6>info6</Info6>  
            <Info7>info7</Info7>  
            <Info8>info8</Info8>  
        </Root>  
Dim doc As XDocument = _   
    <?xml version="1.0" encoding="utf-8" standalone="yes"?>  
        <!--This is a comment-->  
        <Root>  
            <%= From el In srcTree.<Root>.Elements _  
                Where CStr(el).StartsWith("data") _  
                Select el %>  
        </Root>  
Console.WriteLine(doc)  

This example produces the following output:

<!--This is a comment-->  
<Root>  
  <Child1>data1</Child1>  
  <Child2>data2</Child2>  
  <Child3>data3</Child3>  
  <Child2>data4</Child2>  
</Root>  

Remarks

For details about the valid content of an XDocument, see Valid Content of XElement and XDocument Objects.

Constructors

XDocument() XDocument() XDocument()

Initializes a new instance of the XDocument class.

XDocument(Object[]) XDocument(Object[]) XDocument(Object[])

Initializes a new instance of the XDocument class with the specified content.

XDocument(XDeclaration, Object[]) XDocument(XDeclaration, Object[]) XDocument(XDeclaration, Object[])

Initializes a new instance of the XDocument class with the specified XDeclaration and content.

XDocument(XDocument) XDocument(XDocument) XDocument(XDocument)

Initializes a new instance of the XDocument class from an existing XDocument object.

Properties

BaseUri BaseUri BaseUri

Gets the base URI for this XObject.

(Inherited from XObject)
Declaration Declaration Declaration

Gets or sets the XML declaration for this document.

Document Document Document

Gets the XDocument for this XObject.

(Inherited from XObject)
DocumentType DocumentType DocumentType

Gets the Document Type Definition (DTD) for this document.

FirstNode FirstNode FirstNode

Get the first child node of this node.

(Inherited from XContainer)
IXmlLineInfo.LineNumber IXmlLineInfo.LineNumber IXmlLineInfo.LineNumber

Gets the line number that the underlying XmlReader reported for this XObject.

(Inherited from XObject)
IXmlLineInfo.LinePosition IXmlLineInfo.LinePosition IXmlLineInfo.LinePosition

Gets the line position that the underlying XmlReader reported for this XObject.

(Inherited from XObject)
LastNode LastNode LastNode

Get the last child node of this node.

(Inherited from XContainer)
NextNode NextNode NextNode

Gets the next sibling node of this node.

(Inherited from XNode)
NodeType NodeType NodeType

Gets the node type for this node.

Parent Parent Parent

Gets the parent XElement of this XObject.

(Inherited from XObject)
PreviousNode PreviousNode PreviousNode

Gets the previous sibling node of this node.

(Inherited from XNode)
Root Root Root

Gets the root element of the XML Tree for this document.

Methods

Add(Object) Add(Object) Add(Object)

Adds the specified content as children of this XContainer.

(Inherited from XContainer)
Add(Object[]) Add(Object[]) Add(Object[])

Adds the specified content as children of this XContainer.

(Inherited from XContainer)
AddAfterSelf(Object) AddAfterSelf(Object) AddAfterSelf(Object)

Adds the specified content immediately after this node.

(Inherited from XNode)
AddAfterSelf(Object[]) AddAfterSelf(Object[]) AddAfterSelf(Object[])

Adds the specified content immediately after this node.

(Inherited from XNode)
AddAnnotation(Object) AddAnnotation(Object) AddAnnotation(Object)

Adds an object to the annotation list of this XObject.

(Inherited from XObject)
AddBeforeSelf(Object) AddBeforeSelf(Object) AddBeforeSelf(Object)

Adds the specified content immediately before this node.

(Inherited from XNode)
AddBeforeSelf(Object[]) AddBeforeSelf(Object[]) AddBeforeSelf(Object[])

Adds the specified content immediately before this node.

(Inherited from XNode)
AddFirst(Object) AddFirst(Object) AddFirst(Object)

Adds the specified content as the first children of this document or element.

(Inherited from XContainer)
AddFirst(Object[]) AddFirst(Object[]) AddFirst(Object[])

Adds the specified content as the first children of this document or element.

(Inherited from XContainer)
Ancestors() Ancestors() Ancestors()

Returns a collection of the ancestor elements of this node.

(Inherited from XNode)
Ancestors(XName) Ancestors(XName) Ancestors(XName)

Returns a filtered collection of the ancestor elements of this node. Only elements that have a matching XName are included in the collection.

(Inherited from XNode)
Annotation(Type) Annotation(Type) Annotation(Type)

Gets the first annotation object of the specified type from this XObject.

(Inherited from XObject)
Annotation<T>() Annotation<T>() Annotation<T>()

Get the first annotation object of the specified type from this XObject.

(Inherited from XObject)
Annotations(Type) Annotations(Type) Annotations(Type)

Gets a collection of annotations of the specified type for this XObject.

(Inherited from XObject)
Annotations<T>() Annotations<T>() Annotations<T>()

Gets a collection of annotations of the specified type for this XObject.

(Inherited from XObject)
CreateReader() CreateReader() CreateReader()

Creates an XmlReader for this node.

(Inherited from XNode)
CreateReader(ReaderOptions) CreateReader(ReaderOptions) CreateReader(ReaderOptions)

Creates an XmlReader with the options specified by the readerOptions parameter.

(Inherited from XNode)
CreateWriter() CreateWriter() CreateWriter()

Creates an XmlWriter that can be used to add nodes to the XContainer.

(Inherited from XContainer)
DescendantNodes() DescendantNodes() DescendantNodes()

Returns a collection of the descendant nodes for this document or element, in document order.

(Inherited from XContainer)
Descendants() Descendants() Descendants()

Returns a collection of the descendant elements for this document or element, in document order.

(Inherited from XContainer)
Descendants(XName) Descendants(XName) Descendants(XName)

Returns a filtered collection of the descendant elements for this document or element, in document order. Only elements that have a matching XName are included in the collection.

(Inherited from XContainer)
Element(XName) Element(XName) Element(XName)

Gets the first (in document order) child element with the specified XName.

(Inherited from XContainer)
Elements() Elements() Elements()

Returns a collection of the child elements of this element or document, in document order.

(Inherited from XContainer)
Elements(XName) Elements(XName) Elements(XName)

Returns a filtered collection of the child elements of this element or document, in document order. Only elements that have a matching XName are included in the collection.

(Inherited from XContainer)
ElementsAfterSelf() ElementsAfterSelf() ElementsAfterSelf()

Returns a collection of the sibling elements after this node, in document order.

(Inherited from XNode)
ElementsAfterSelf(XName) ElementsAfterSelf(XName) ElementsAfterSelf(XName)

Returns a filtered collection of the sibling elements after this node, in document order. Only elements that have a matching XName are included in the collection.

(Inherited from XNode)
ElementsBeforeSelf() ElementsBeforeSelf() ElementsBeforeSelf()

Returns a collection of the sibling elements before this node, in document order.

(Inherited from XNode)
ElementsBeforeSelf(XName) ElementsBeforeSelf(XName) ElementsBeforeSelf(XName)

Returns a filtered collection of the sibling elements before this node, in document order. Only elements that have a matching XName are included in the collection.

(Inherited from XNode)
Equals(Object) Equals(Object) Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType()

Gets the Type of the current instance.

(Inherited from Object)
IsAfter(XNode) IsAfter(XNode) IsAfter(XNode)

Determines if the current node appears after a specified node in terms of document order.

(Inherited from XNode)
IsBefore(XNode) IsBefore(XNode) IsBefore(XNode)

Determines if the current node appears before a specified node in terms of document order.

(Inherited from XNode)
Load(Stream) Load(Stream) Load(Stream)

Creates a new XDocument instance by using the specified stream.

Load(Stream, LoadOptions) Load(Stream, LoadOptions) Load(Stream, LoadOptions)

Creates a new XDocument instance by using the specified stream, optionally preserving white space, setting the base URI, and retaining line information.

Load(String) Load(String) Load(String)

Creates a new XDocument from a file.

Load(String, LoadOptions) Load(String, LoadOptions) Load(String, LoadOptions)

Creates a new XDocument from a file, optionally preserving white space, setting the base URI, and retaining line information.

Load(TextReader) Load(TextReader) Load(TextReader)

Creates a new XDocument from a TextReader.

Load(TextReader, LoadOptions) Load(TextReader, LoadOptions) Load(TextReader, LoadOptions)

Creates a new XDocument from a TextReader, optionally preserving white space, setting the base URI, and retaining line information.

Load(XmlReader) Load(XmlReader) Load(XmlReader)

Creates a new XDocument from an XmlReader.

Load(XmlReader, LoadOptions) Load(XmlReader, LoadOptions) Load(XmlReader, LoadOptions)

Loads an XDocument from an XmlReader, optionally setting the base URI, and retaining line information.

LoadAsync(Stream, LoadOptions, CancellationToken) LoadAsync(Stream, LoadOptions, CancellationToken) LoadAsync(Stream, LoadOptions, CancellationToken)
LoadAsync(TextReader, LoadOptions, CancellationToken) LoadAsync(TextReader, LoadOptions, CancellationToken) LoadAsync(TextReader, LoadOptions, CancellationToken)
LoadAsync(XmlReader, LoadOptions, CancellationToken) LoadAsync(XmlReader, LoadOptions, CancellationToken) LoadAsync(XmlReader, LoadOptions, CancellationToken)
MemberwiseClone() MemberwiseClone() MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
Nodes() Nodes() Nodes()

Returns a collection of the child nodes of this element or document, in document order.

(Inherited from XContainer)
NodesAfterSelf() NodesAfterSelf() NodesAfterSelf()

Returns a collection of the sibling nodes after this node, in document order.

(Inherited from XNode)
NodesBeforeSelf() NodesBeforeSelf() NodesBeforeSelf()

Returns a collection of the sibling nodes before this node, in document order.

(Inherited from XNode)
Parse(String) Parse(String) Parse(String)

Creates a new XDocument from a string.

Parse(String, LoadOptions) Parse(String, LoadOptions) Parse(String, LoadOptions)

Creates a new XDocument from a string, optionally preserving white space, setting the base URI, and retaining line information.

Remove() Remove() Remove()

Removes this node from its parent.

(Inherited from XNode)
RemoveAnnotations(Type) RemoveAnnotations(Type) RemoveAnnotations(Type)

Removes the annotations of the specified type from this XObject.

(Inherited from XObject)
RemoveAnnotations<T>() RemoveAnnotations<T>() RemoveAnnotations<T>()

Removes the annotations of the specified type from this XObject.

(Inherited from XObject)
RemoveNodes() RemoveNodes() RemoveNodes()

Removes the child nodes from this document or element.

(Inherited from XContainer)
ReplaceNodes(Object) ReplaceNodes(Object) ReplaceNodes(Object)

Replaces the children nodes of this document or element with the specified content.

(Inherited from XContainer)
ReplaceNodes(Object[]) ReplaceNodes(Object[]) ReplaceNodes(Object[])

Replaces the children nodes of this document or element with the specified content.

(Inherited from XContainer)
ReplaceWith(Object) ReplaceWith(Object) ReplaceWith(Object)

Replaces this node with the specified content.

(Inherited from XNode)
ReplaceWith(Object[]) ReplaceWith(Object[]) ReplaceWith(Object[])

Replaces this node with the specified content.

(Inherited from XNode)
Save(Stream) Save(Stream) Save(Stream)

Outputs this XDocument to the specified Stream.

Save(Stream, SaveOptions) Save(Stream, SaveOptions) Save(Stream, SaveOptions)

Outputs this XDocument to the specified Stream, optionally specifying formatting behavior.

Save(String) Save(String) Save(String)

Serialize this XDocument to a file, overwriting an existing file, if it exists.

Save(String, SaveOptions) Save(String, SaveOptions) Save(String, SaveOptions)

Serialize this XDocument to a file, optionally disabling formatting.

Save(TextWriter) Save(TextWriter) Save(TextWriter)

Serialize this XDocument to a TextWriter.

Save(TextWriter, SaveOptions) Save(TextWriter, SaveOptions) Save(TextWriter, SaveOptions)

Serialize this XDocument to a TextWriter, optionally disabling formatting.

Save(XmlWriter) Save(XmlWriter) Save(XmlWriter)

Serialize this XDocument to an XmlWriter.

SaveAsync(Stream, SaveOptions, CancellationToken) SaveAsync(Stream, SaveOptions, CancellationToken) SaveAsync(Stream, SaveOptions, CancellationToken)
SaveAsync(TextWriter, SaveOptions, CancellationToken) SaveAsync(TextWriter, SaveOptions, CancellationToken) SaveAsync(TextWriter, SaveOptions, CancellationToken)
SaveAsync(XmlWriter, CancellationToken) SaveAsync(XmlWriter, CancellationToken) SaveAsync(XmlWriter, CancellationToken)
ToString() ToString() ToString()

Returns the indented XML for this node.

(Inherited from XNode)
ToString(SaveOptions) ToString(SaveOptions) ToString(SaveOptions)

Returns the XML for this node, optionally disabling formatting.

(Inherited from XNode)
WriteTo(XmlWriter) WriteTo(XmlWriter) WriteTo(XmlWriter)

Write this document to an XmlWriter.

WriteToAsync(XmlWriter, CancellationToken) WriteToAsync(XmlWriter, CancellationToken) WriteToAsync(XmlWriter, CancellationToken)

Events

Changed Changed Changed

Raised when this XObject or any of its descendants have changed.

(Inherited from XObject)
Changing Changing Changing

Raised when this XObject or any of its descendants are about to change.

(Inherited from XObject)

Explicit Interface Implementations

IXmlLineInfo.HasLineInfo() IXmlLineInfo.HasLineInfo() IXmlLineInfo.HasLineInfo()

Gets a value indicating whether or not this XObject has line information.

(Inherited from XObject)

Extension Methods

Validate(XDocument, XmlSchemaSet, ValidationEventHandler) Validate(XDocument, XmlSchemaSet, ValidationEventHandler) Validate(XDocument, XmlSchemaSet, ValidationEventHandler)

This method validates that an XDocument conforms to an XSD in an XmlSchemaSet.

Validate(XDocument, XmlSchemaSet, ValidationEventHandler, Boolean) Validate(XDocument, XmlSchemaSet, ValidationEventHandler, Boolean) Validate(XDocument, XmlSchemaSet, ValidationEventHandler, Boolean)

Validates that an XDocument conforms to an XSD in an XmlSchemaSet, optionally populating the XML tree with the post-schema-validation infoset (PSVI).

CreateNavigator(XNode) CreateNavigator(XNode) CreateNavigator(XNode)

Creates an XPathNavigator for an XNode.

CreateNavigator(XNode, XmlNameTable) CreateNavigator(XNode, XmlNameTable) CreateNavigator(XNode, XmlNameTable)

Creates an XPathNavigator for an XNode. The XmlNameTable enables more efficient XPath expression processing.

XPathEvaluate(XNode, String) XPathEvaluate(XNode, String) XPathEvaluate(XNode, String)

Evaluates an XPath expression.

XPathEvaluate(XNode, String, IXmlNamespaceResolver) XPathEvaluate(XNode, String, IXmlNamespaceResolver) XPathEvaluate(XNode, String, IXmlNamespaceResolver)

Evaluates an XPath expression, resolving namespace prefixes using the specified IXmlNamespaceResolver.

XPathSelectElement(XNode, String) XPathSelectElement(XNode, String) XPathSelectElement(XNode, String)

Selects an XElement using a XPath expression.

XPathSelectElement(XNode, String, IXmlNamespaceResolver) XPathSelectElement(XNode, String, IXmlNamespaceResolver) XPathSelectElement(XNode, String, IXmlNamespaceResolver)

Selects an XElement using a XPath expression, resolving namespace prefixes using the specified IXmlNamespaceResolver.

XPathSelectElements(XNode, String) XPathSelectElements(XNode, String) XPathSelectElements(XNode, String)

Selects a collection of elements using an XPath expression.

XPathSelectElements(XNode, String, IXmlNamespaceResolver) XPathSelectElements(XNode, String, IXmlNamespaceResolver) XPathSelectElements(XNode, String, IXmlNamespaceResolver)

Selects a collection of elements using an XPath expression, resolving namespace prefixes using the specified IXmlNamespaceResolver.

ToXPathNavigable(XNode) ToXPathNavigable(XNode) ToXPathNavigable(XNode)

Returns an accessor that allows you to navigate and edit the specified XNode.

Applies to

See Also