XNode.CreateReader Metodo

Definizione

Crea un oggetto XmlReader per questo nodo.

Overload

CreateReader()

Crea un oggetto XmlReader per questo nodo.

CreateReader(ReaderOptions)

Crea un oggetto XmlReader con le opzioni specificate dal parametro readerOptions.

CreateReader()

Crea un oggetto XmlReader per questo nodo.

public:
 System::Xml::XmlReader ^ CreateReader();
public System.Xml.XmlReader CreateReader ();
member this.CreateReader : unit -> System.Xml.XmlReader
Public Function CreateReader () As XmlReader

Restituisce

XmlReader

XmlReader che può essere utilizzato per leggere questo nodo e i relativi discendenti.

Esempio

L'esempio seguente crea un albero XML, crea un XmlReader oggetto usando il CreateReader metodo e crea un XmlDocument oggetto usando il lettore.

XDocument xmlTree = new XDocument(  
    new XElement("Root",  
        new XAttribute("Att1", "Attribute Content"),  
        new XElement("Child1", 1),  
        new XElement("Child2", 2)  
    )  
);  
XmlReader reader = xmlTree.CreateReader();  
reader.MoveToContent();  
XmlDocument doc = new XmlDocument();  
XmlNode cd = doc.ReadNode(reader);  
doc.AppendChild(cd);  
Console.WriteLine(doc.OuterXml);  
Dim xmlTree As XDocument =  _   
    <?xml version="1.0" encoding="utf-8" standalone="yes"?>  
        <Root Att1="Attribute Content">  
            <Child1>1</Child1>  
            <Child2>2</Child2>  
        </Root>  
Dim reader As XmlReader = xmlTree.CreateReader()  
reader.MoveToContent()  
Dim doc As XmlDocument = New XmlDocument()  
Dim cd As XmlNode = doc.ReadNode(reader)  
doc.AppendChild(cd)  
Console.WriteLine(doc.OuterXml)  

Nell'esempio viene prodotto l'output seguente:

<Root Att1="Attribute Content"><Child1>1</Child1><Child2>2</Child2></Root>  

Un altro uso per questo metodo consiste nell'eseguire una trasformazione XSLT. È possibile creare un albero XML, creare un oggetto XmlReader dall'albero XML, creare un nuovo documento e infine creare un oggetto XmlWriter che scriverà nel documento nuovo. Quindi, è possibile richiamare la trasformazione XSLT, passando XmlReader e XmlWriter alla trasformazione. Dopo il completamento della trasformazione, il nuovo albero XML viene popolato con i relativi risultati.

string xslMarkup = @"<?xml version='1.0'?>  
<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='1.0'>  
    <xsl:template match='/Parent'>  
        <Root>  
            <C1>  
            <xsl:value-of select='Child1'/>  
            </C1>  
            <C2>  
            <xsl:value-of select='Child2'/>  
            </C2>  
        </Root>  
    </xsl:template>  
</xsl:stylesheet>";  

XDocument xmlTree = new XDocument(  
    new XElement("Parent",  
        new XElement("Child1", "Child1 data"),  
        new XElement("Child2", "Child2 data")  
    )  
);  

XDocument newTree = new XDocument();  
using (XmlWriter writer = newTree.CreateWriter()) {  
    // Load the style sheet.  
    XslCompiledTransform xslt = new XslCompiledTransform();  
    xslt.Load(XmlReader.Create(new StringReader(xslMarkup)));  

    // Execute the transform and output the results to a writer.  
    xslt.Transform(xmlTree.CreateReader(), writer);  
}  

Console.WriteLine(newTree);  
Dim xslMarkup As XDocument = _   
    <?xml version='1.0'?>  
    <xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='1.0'>  
        <xsl:template match='/Parent'>  
            <Root>  
                <C1>  
                    <xsl:value-of select='Child1'/>  
                </C1>  
                <C2>  
                    <xsl:value-of select='Child2'/>  
                </C2>  
            </Root>  
        </xsl:template>  
    </xsl:stylesheet>  

Dim xmlTree As XElement = _   
        <Parent>  
            <Child1>Child1 data</Child1>  
            <Child2>Child2 data</Child2>  
        </Parent>  

Dim newTree As XDocument = New XDocument()  

Using writer As XmlWriter = newTree.CreateWriter()  
    ' Load the style sheet.  
    Dim xslt As XslCompiledTransform = _  
        New XslCompiledTransform()  
    xslt.Load(xslMarkup.CreateReader())  

    ' Execute the transform and output the results to a writer.  
    xslt.Transform(xmlTree.CreateReader(), writer)  
End Using  

Console.WriteLine(newTree)  

Nell'esempio viene prodotto l'output seguente:

<Root>  
  <C1>Child1 data</C1>  
  <C2>Child2 data</C2>  
</Root>  

Commenti

In genere si usa questo metodo quando è necessario specificare un altro componente con un XmlReaderoggetto . Ad esempio, è possibile creare un oggetto XmlReader da un albero LINQ to XML e quindi passare tale lettore a Load.

Tutti i lettori restituiti da Create sono lettori normalizzabili. Eseguono sempre la normalizzazione delle interruzioni di riga e la normalizzazione completa degli attributi. Al contrario, il XmlReader restituito da CreateReader non è un lettore normalizzatore. Non trasforma spazi vuoti. Restituisce anche gli attributi nell'ordine in cui sono stati aggiunti, non nell'ordine dei nomi degli attributi.

LINQ to XML non mantiene informazioni sul fatto che gli attributi siano attributi predefiniti. IsDefault restituisce sempre false indipendentemente dal fatto che l'attributo sia stato popolato da un valore predefinito o meno.

Gli PUBLIC attributi pseudo e SYSTEM su XDocumentType non sono disponibili tramite il XmlReader.MoveToAttribute metodo . Sono disponibili solo tramite il XmlReader.GetAttribute metodo che accetta il nome qualificato dell'attributo come parametro. Se è necessario recuperare gli PUBLIC attributi o SYSTEM , è consigliabile usare il XmlReader.GetAttribute metodo .

I dati Base64 e BinHex non sono supportati. Se si tenta di recuperare questi tipi di dati, ad esempio chiamando ReadElementContentAsBase64, il lettore genererà NotSupportedException.

La xml dichiarazione non viene superficieta dal lettore. Durante la lettura, non verrà rilevato un nodo di tipo XmlDeclaration.

Vedi anche

Si applica a

CreateReader(ReaderOptions)

Crea un oggetto XmlReader con le opzioni specificate dal parametro readerOptions.

public:
 System::Xml::XmlReader ^ CreateReader(System::Xml::Linq::ReaderOptions readerOptions);
public System.Xml.XmlReader CreateReader (System.Xml.Linq.ReaderOptions readerOptions);
member this.CreateReader : System.Xml.Linq.ReaderOptions -> System.Xml.XmlReader
Public Function CreateReader (readerOptions As ReaderOptions) As XmlReader

Parametri

readerOptions
ReaderOptions

Oggetto ReaderOptions che specifica se omettere gli spazi dei nomi duplicati.

Restituisce

XmlReader

Oggetto XmlReader.

Si applica a