Cet article a fait l’objet d’une traduction automatique. Pour afficher l’article en anglais, activez la case d’option Anglais. Vous pouvez également afficher le texte anglais dans une fenêtre contextuelle en faisant glisser le pointeur de la souris sur le texte traduit.
Traduction
Anglais

XDocument.Load, méthode (XmlReader, LoadOptions)

 

Charge un XDocument à partir d'un XmlReader, en définissant éventuellement l'URI de base et en conservant les informations de ligne.

Espace de noms:   System.Xml.Linq
Assembly:  System.Xml.Linq (dans System.Xml.Linq.dll)

public static XDocument Load(
	XmlReader reader,
	LoadOptions options
)

Paramètres

reader

XmlReader qui sera lu pour le contenu du XDocument.

options

LoadOptions qui spécifie s'il faut charger l'URI de base et les informations de ligne.

Valeur de retour

Type: System.Xml.Linq.XDocument

XDocument contenant le code XML qui a été lu à partir du XmlReader spécifié.

By creating an XmlNodeReader from a DOM document, and then using the XmlNodeReader to create an XElement, this method can be used to create a copy of a DOM document in a LINQ to XML tree.

Use Parse to create an XDocument from a string that contains XML.

Setting PreserveWhitespace is not valid when loading from a XmlReader.The XmlReader will be configured to either read whitespace or not.The LINQ to XML tree will be populated with the whitespace nodes that the reader surfaces.This will be the behavior regardless of whether PreserveWhitespace is set or not.

The XmlReader may have a valid base URI or not.If you set SetBaseUri, the base URI will be set in the XML tree from the base URI that is reported by the XmlReader.

The XmlReader may have a valid line information or not.If you set SetLineInfo, the line information will be set in the XML tree from the line information that is reported by the XmlReader.

There is a performance penalty if you set the SetLineInfo flag.

The line information is accurate immediately after loading the XML document.If you modify the XML tree after loading the document, the line information may become meaningless.

LINQ to XML's loading functionality is built upon XmlReader.Therefore, you might catch any exceptions that are thrown by the XmlReader.Create overload methods and the XmlReader methods that read and parse the document.

The following example loads the line information that it loads from the XmlReader.It then prints the line information.

string markup =
@"<Root>
    <Child>
        <GrandChild/>
    </Child>
</Root>";

// Create a reader and move to the content.
using (XmlReader nodeReader = XmlReader.Create(new StringReader(markup)))
{
    // the reader must be in the Interactive state in order to
    // Create a LINQ to XML tree from it.
    nodeReader.MoveToContent();

    XDocument xRoot = XDocument.Load(nodeReader, LoadOptions.SetLineInfo);
    Console.WriteLine("{0}{1}{2}",
        "Element Name".PadRight(20),
        "Line".PadRight(5),
        "Position");
    Console.WriteLine("{0}{1}{2}",
        "------------".PadRight(20),
        "----".PadRight(5),
        "--------");
    foreach (XElement e in xRoot.Elements("Root").DescendantsAndSelf())
        Console.WriteLine("{0}{1}{2}",
            ("".PadRight(e.Ancestors().Count() * 2) + e.Name).PadRight(20),
            ((IXmlLineInfo)e).LineNumber.ToString().PadRight(5),
            ((IXmlLineInfo)e).LinePosition);
}

This example produces the following output:

Element Name        Line Position
------------        ---- --------
Root                1    2
  Child             2    6
    GrandChild      3    10

Universal Windows Platform
Disponible depuis 4.5
.NET Framework
Disponible depuis 3.5
Portable Class Library
Pris en charge dans : portable .NET platforms
Silverlight
Disponible depuis 2.0
Windows Phone Silverlight
Disponible depuis 7.0
Windows Phone
Disponible depuis 8.1
Retour au début
Afficher: