Extensions.CreateNavigator 方法

定义

XPathNavigator 创建一个 XNode

重载

CreateNavigator(XNode)

XPathNavigator 创建一个 XNode

CreateNavigator(XNode, XmlNameTable)

XPathNavigator 创建一个 XNodeXmlNameTable 可以使 XPath 表达式的处理变得更高效。

注解

不能使用 XPathNavigator 此方法返回的 XML 树进行编辑。 CanEdit 属性返回 false

不能为节点创建一个XPathNavigatorXDocumentType。 文档类型不参与 XPath 数据模型。

命名空间声明从左到右报告。 相比之下, XmlDocument命名空间从右向左报告。 这是一致性行为,因为命名空间声明在 XPath 数据模型中没有排序。

MoveToId此方法返回的导航器不支持该方法。

可以使用此方法执行 XSLT 转换。 可以创建 XML 树,从 XML 树创建一 XPathNavigator 个,创建新的文档,并创建一个 XmlWriter 将写入到新文档中的文档。 然后,您可以调用 XSLT 转换,并可以将 XPathNavigatorXmlWriter 传递到转换中。 在转换成功完成后,使用转换的结果,填充新的 XML 树。

若要执行 XSLT 转换,可以使用一个或一个 XmlReader XPathNavigator。 这两种方法具有不同的性能特征。 某些转换在使用时 XmlReader执行速度更快,而其他转换在使用时 XPathNavigator执行速度更快。 如果性能是个问题,我们建议你尝试每个方法来确定在情况中性能更好。

CreateNavigator(XNode)

XPathNavigator 创建一个 XNode

public:
[System::Runtime::CompilerServices::Extension]
 static System::Xml::XPath::XPathNavigator ^ CreateNavigator(System::Xml::Linq::XNode ^ node);
public static System.Xml.XPath.XPathNavigator CreateNavigator (this System.Xml.Linq.XNode node);
static member CreateNavigator : System.Xml.Linq.XNode -> System.Xml.XPath.XPathNavigator
<Extension()>
Public Function CreateNavigator (node As XNode) As XPathNavigator

参数

node
XNode

一个可以处理 XPath 查询的 XNode

返回

XPathNavigator

一个可以处理 XPath 查询的 XPathNavigator

示例

using System.Xml;
using System.Xml.Linq;
using System.Xml.XPath;
using System.Xml.Xsl;

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(
    new XElement("Parent",
        new XElement("Child1", "Child1 data"),
        new XElement("Child2", "Child2 data")
    )
);

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

    // Execute the transform and output the results to a writer.  
    xslt.Transform(xmlTree.CreateNavigator(), 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 XDocument = _  
    <?xml version='1.0'?>  
    <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.CreateNavigator(), writer)  
End Using  

Console.WriteLine(newTree)  

该示例产生下面的输出:

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

注解

不能使用此方法返回的 XPathNavigator XML 树进行编辑。 CanEdit 属性返回 false

不能为节点创建一个XPathNavigatorXDocumentType。 文档类型不参与 XPath 数据模型。

命名空间声明从左到右报告。 相比之下,命名空间 XmlDocument 从右向左报告。 这是一致性行为,因为命名空间声明在 XPath 数据模型中没有排序。

MoveToId此方法返回的导航器不支持该方法。

可以使用此方法执行 XSLT 转换。 可以创建 XML 树,从 XML 树创建一 XPathNavigator 个,创建新的文档,并创建一个 XmlWriter 将写入到新文档中的文档。 然后,可以调用 XSLT 转换,并将转换XmlWriter传递给XPathNavigator转换。 在转换成功完成后,使用转换的结果,填充新的 XML 树。

若要执行 XSLT 转换,可以使用一个或一个 XmlReader XPathNavigator。 这两种方法具有不同的性能特征。 某些转换在使用时 XmlReader执行速度更快,而其他转换在使用时 XPathNavigator执行速度更快。 如果性能是个问题,我们建议你尝试每个方法来确定在情况中性能更好。

适用于

CreateNavigator(XNode, XmlNameTable)

XPathNavigator 创建一个 XNodeXmlNameTable 可以使 XPath 表达式的处理变得更高效。

public:
[System::Runtime::CompilerServices::Extension]
 static System::Xml::XPath::XPathNavigator ^ CreateNavigator(System::Xml::Linq::XNode ^ node, System::Xml::XmlNameTable ^ nameTable);
public static System.Xml.XPath.XPathNavigator CreateNavigator (this System.Xml.Linq.XNode node, System.Xml.XmlNameTable? nameTable);
public static System.Xml.XPath.XPathNavigator CreateNavigator (this System.Xml.Linq.XNode node, System.Xml.XmlNameTable nameTable);
static member CreateNavigator : System.Xml.Linq.XNode * System.Xml.XmlNameTable -> System.Xml.XPath.XPathNavigator
<Extension()>
Public Function CreateNavigator (node As XNode, nameTable As XmlNameTable) As XPathNavigator

参数

node
XNode

一个可以处理 XPath 查询的 XNode

nameTable
XmlNameTable

一个将由 XmlNameTable 使用的 XPathNavigator

返回

XPathNavigator

一个可以处理 XPath 查询的 XPathNavigator

注解

不能使用此方法返回的 XPathNavigator XML 树进行编辑。 CanEdit 属性返回 false

不能为节点创建一个XPathNavigatorXDocumentType。 文档类型不参与 XPath 数据模型。

命名空间声明从左到右报告。 相比之下,命名空间 XmlDocument 从右向左报告。 这是一致性行为,因为命名空间声明在 XPath 数据模型中没有排序。

MoveToId此方法返回的导航器不支持该方法。

如果使用 XmlNameTable 此方法来创建此方法 XPathNavigator,则计算 XPath 表达式时将得到更好的性能。

适用于