XNode.CreateReader 方法

定义

创建此节点的 XmlReaderCreates an XmlReader for this node.

重载

CreateReader()

创建此节点的 XmlReaderCreates an XmlReader for this node.

CreateReader(ReaderOptions)

使用 readerOptions 参数指定的选项创建 XmlReaderCreates an XmlReader with the options specified by the readerOptions parameter.

CreateReader()

创建此节点的 XmlReaderCreates an XmlReader for this node.

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

返回

可用于读取此节点及其子代的 XmlReaderAn XmlReader that can be used to read this node and its descendants.

示例

下面的示例创建一个 XML 树,使用 CreateReader 方法创建 XmlReader,并使用读取器创建一个 XmlDocumentThe following example creates an XML tree, creates an XmlReader by using the CreateReader method, and creates an XmlDocument by using the reader.

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)  

该示例产生下面的输出:This example produces the following output:

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

此方法的另一个用途是执行 XSLT 转换。Another use for this method is to do an XSLT transformation. 可以创建 XML 树,从 XML 树创建 XmlReader,创建新文档,然后创建 XmlWriter,以写入新文档。You can create an XML tree, create an XmlReader from the XML tree, create a new document, and create an XmlWriter that will write into the new document. 然后,您可以调用 XSLT 转换,并可以将 XmlReaderXmlWriter 传递到转换中。Then, you can invoke the XSLT transformation, passing the XmlReader and XmlWriter to the transformation. 在转换成功完成后,使用转换的结果,填充新的 XML 树。After the transformation successfully completes, the new XML tree is populated with the results of the transform.

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)  

该示例产生下面的输出:This example produces the following output:

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

注解

当必须提供具有 XmlReader的其他组件时,通常使用此方法。You typically use this method when you have to supply another component with an XmlReader. 例如,你可以从 LINQ to XMLLINQ to XML 树创建 XmlReader,然后将该读取器传递到 LoadFor example, you can create an XmlReader from a LINQ to XMLLINQ to XML tree, and then pass that reader to Load.

Create 返回的所有读取器均为规范化读取器。All of the readers returned by Create are normalizing readers. 它们始终执行分行规范化和属性的完全规范化。They always perform line break normalization and full normalization of attributes. 相反,CreateReader 返回的 XmlReader 不是规范化读取器。In contrast, the XmlReader returned by CreateReader is not a normalizing reader. 它不会转换任何空白。It does not transform any white space. 它还按它们的添加顺序返回属性,而不是按属性名称顺序返回。It also returns attributes in the order that they were added, not in attribute name order.

LINQ to XMLLINQ to XML 不会保留有关属性是否为默认属性的信息。does not keep information about whether attributes are default attributes. 无论是否从默认值填充属性,IsDefault 都将始终返回 false。IsDefault will always return false regardless of whether the attribute was populated from a default value or not.

XDocumentType 上的 PUBLICSYSTEM 伪特性无法通过 XmlReader.MoveToAttribute 方法获取。The PUBLIC and SYSTEM pseudo attributes on XDocumentType are not available through the XmlReader.MoveToAttribute method. 仅可通过将属性的限定名称作为参数的 XmlReader.GetAttribute 方法来使用它们。They are only available through the XmlReader.GetAttribute method that takes the qualified name of the attribute as a parameter. 如果必须检索 PUBLICSYSTEM 属性,则应使用 XmlReader.GetAttribute 方法。If you have to retrieve the PUBLIC or SYSTEM attributes, you should use the XmlReader.GetAttribute method.

Base64 和 BinHex 数据不受支持。Base64 and BinHex data are not supported. 如果尝试检索这些数据类型(例如,通过调用 ReadElementContentAsBase64),读取器将引发 NotSupportedExceptionIf you attempt to retrieve these types of data (for example, by calling ReadElementContentAsBase64), the reader will throw NotSupportedException.

读取器不会呈现 xml 声明。The xml declaration is not surfaced by the reader. 在读取过程中,不会遇到 XmlDeclaration类型的节点。While reading, you will not encounter a node of type XmlDeclaration.

另请参阅

CreateReader(ReaderOptions)

使用 readerOptions 参数指定的选项创建 XmlReaderCreates an XmlReader with the options specified by the readerOptions parameter.

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

参数

readerOptions
ReaderOptions

指定是否省略重复的命名空间的 ReaderOptions 对象。A ReaderOptions object that specifies whether to omit duplicate namespaces.

返回

一个 XmlReader 对象。An XmlReader object.

适用于