Condividi tramite


XNode.CreateReader Metodo

Definizione

Crea un oggetto XmlReader per questo nodo.

Overload

Nome Descrizione
CreateReader()

Crea un oggetto XmlReader per questo nodo.

CreateReader(ReaderOptions)

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

CreateReader()

Origine:
XNode.cs
Origine:
XNode.cs
Origine:
XNode.cs
Origine:
XNode.cs
Origine:
XNode.cs

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

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

Esempio

Nell'esempio seguente viene creato un albero XML, viene creato un XmlReader oggetto utilizzando il CreateReader metodo e viene creato un XmlDocument oggetto utilizzando 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)

In questo esempio viene generato 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, crearne uno XmlReader dall'albero XML, creare un nuovo documento e creare un oggetto XmlWriter che scriverà nel nuovo documento. È quindi possibile richiamare la trasformazione XSLT, passando e XmlReaderXmlWriter alla trasformazione . Al termine della trasformazione, il nuovo albero XML viene popolato con i risultati della trasformazione.

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)

In questo esempio viene generato l'output seguente:

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

Commenti

Questo metodo viene in genere usato quando è necessario fornire un altro componente con un oggetto XmlReader. 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 normalizzano i lettori. Eseguono sempre la normalizzazione dell'interruzione di riga e la normalizzazione completa degli attributi. Al contrario, l'oggetto 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 restituirà sempre false indipendentemente dal fatto che l'attributo sia stato popolato da un valore predefinito o meno.

Gli PUBLIC attributi pseudo e SYSTEM in XDocumentType non sono disponibili tramite il XmlReader.MoveToAttribute metodo . Sono disponibili solo tramite il XmlReader.GetAttribute metodo che accetta il nome completo dell'attributo come parametro. Se è necessario recuperare gli PUBLIC attributi o SYSTEM , è necessario 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 rilevata dal lettore. Durante la lettura, non verrà rilevato un nodo di tipo XmlDeclaration.

Vedi anche

Si applica a

CreateReader(ReaderOptions)

Origine:
XNode.cs
Origine:
XNode.cs
Origine:
XNode.cs
Origine:
XNode.cs
Origine:
XNode.cs

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

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 spazi dei nomi duplicati.

Restituisce

Oggetto XmlReader.

Si applica a