Extensions.CreateNavigator 方法

定義

建立 XPathNavigatorXNodeCreates an XPathNavigator for an XNode.

多載

CreateNavigator(XNode)

建立 XPathNavigatorXNodeCreates an XPathNavigator for an XNode.

CreateNavigator(XNode, XmlNameTable)

建立 XPathNavigatorXNodeCreates an XPathNavigator for an XNode. XmlNameTable 可讓 XPath 運算式處理時更有效率。The XmlNameTable enables more efficient XPath expression processing.

備註

您無法使用此方法所傳回的 XPathNavigator 來編輯 XML 樹狀結構。You cannot edit the XML tree by using the XPathNavigator that is returned by this method. CanEdit 屬性會傳回 falseThe CanEdit property returns false.

您無法建立 XDocumentType 節點的 XPathNavigatorYou cannot create an XPathNavigator for a XDocumentType node. 檔案類型不會參與 XPath 資料模型。Document types do not participate in the XPath data model.

命名空間宣告由左至右回報。Namespace declarations are reported from left to right. 相反地,針對 XmlDocument 的命名空間會從右至左回報。In contrast, for XmlDocument namespaces are reported from right to left. 這是一致的行為,因為不會在 XPath 資料模型中排序命名空間宣告。This is conformant behavior because namespace declarations are not ordered in the XPath data model.

這個方法所傳回的導覽器不支援方法 MoveToIdThe method MoveToId is not supported for navigators that are returned by this method.

您可以使用這個方法來執行 XSLT 轉換。You can use this method to perform an XSLT transformation. 您可以建立 XML 樹狀結構、從 XML 樹狀結構建立 XPathNavigator、建立新檔,然後建立將寫入新檔的 XmlWriterYou can create an XML tree, create an XPathNavigator from the XML tree, create a new document, and create a XmlWriter that will write into the new document. 然後,您可以叫用 (Invoke) XSLT 轉換,將 XPathNavigatorXmlWriter 傳遞至轉換。Then, you can invoke the XSLT transformation, passing the XPathNavigator and XmlWriter to the transformation. 轉換成功完成後,系統會使用轉換的結果填入新的 XML 樹狀結構。After the transformation successfully completes, the new XML tree is populated with the results of the transformation.

若要執行 XSLT 轉換,您可以使用 XmlReaderXPathNavigatorTo perform an XSLT transformation, you can use either an XmlReader or an XPathNavigator. 這兩種方法具有不同的效能特性。The two approaches have different performance characteristics. 當使用 XmlReader時,某些轉換的執行速度會更快,有些則會在使用 XPathNavigator時更快執行。Some transformations will execute faster when using an XmlReader, and others will execute faster when using a XPathNavigator. 如果效能是一項考慮,我們建議您試驗每一種方法,以判斷在您的情況下可執行得更佳。If performance is a concern, we recommend that you experiment with each approach to determine which will perform better in your circumstances.

  
            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.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)  

這個範例會產生下列輸出:This example produces the following output:

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

CreateNavigator(XNode)

建立 XPathNavigatorXNodeCreates an XPathNavigator for an 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 查詢的 XNodeAn XNode that can process XPath queries.

傳回

XPathNavigator

可處理 XPath 查詢的 XPathNavigatorAn XPathNavigator that can process XPath queries.

範例

  
                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.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)  

這個範例會產生下列輸出:This example produces the following output:

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

備註

您無法使用此方法所傳回的 XPathNavigator 來編輯 XML 樹狀結構。You cannot edit the XML tree by using the XPathNavigator that is returned by this method. CanEdit 屬性會傳回 falseThe CanEdit property returns false.

您無法建立 XDocumentType 節點的 XPathNavigatorYou cannot create an XPathNavigator for a XDocumentType node. 檔案類型不會參與 XPath 資料模型。Document types do not participate in the XPath data model.

命名空間宣告由左至右回報。Namespace declarations are reported from left to right. 相反地,針對 XmlDocument 的命名空間會從右至左回報。In contrast, for XmlDocument namespaces are reported from right to left. 這是一致的行為,因為不會在 XPath 資料模型中排序命名空間宣告。This is conformant behavior because namespace declarations are not ordered in the XPath data model.

這個方法所傳回的導覽器不支援方法 MoveToIdThe method MoveToId is not supported for navigators that are returned by this method.

您可以使用這個方法來執行 XSLT 轉換。You can use this method to perform an XSLT transformation. 您可以建立 XML 樹狀結構、從 XML 樹狀結構建立 XPathNavigator、建立新檔,然後建立將寫入新檔的 XmlWriterYou can create an XML tree, create an XPathNavigator from the XML tree, create a new document, and create a XmlWriter that will write into the new document. 然後,您可以叫用 XSLT 轉換,將 XPathNavigatorXmlWriter 傳遞至轉換。Then, you can invoke the XSLT transformation, passing the XPathNavigator and XmlWriter to the transform. 轉換成功完成後,系統會使用轉換的結果填入新的 XML 樹狀結構。After the transformation successfully completes, the new XML tree is populated with the results of the transformation.

若要執行 XSLT 轉換,您可以使用 XmlReaderXPathNavigatorTo perform an XSLT transformation, you can use either an XmlReader or an XPathNavigator. 這兩種方法具有不同的效能特性。The two approaches have different performance characteristics. 當使用 XmlReader時,某些轉換的執行速度會更快,有些則會在使用 XPathNavigator時更快執行。Some transformations will execute faster when using an XmlReader, and others will execute faster when using a XPathNavigator. 如果效能是一項考慮,我們建議您試驗每一種方法,以判斷在您的情況下可執行得更佳。If performance is a concern, we recommend that you experiment with each approach to determine which will perform better in your circumstances.

CreateNavigator(XNode, XmlNameTable)

建立 XPathNavigatorXNodeCreates an XPathNavigator for an XNode. XmlNameTable 可讓 XPath 運算式處理時更有效率。The XmlNameTable enables more efficient XPath expression processing.

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);
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 查詢的 XNodeAn XNode that can process an XPath query.

nameTable
XmlNameTable

XmlNameTable 要使用的 XPathNavigatorA XmlNameTable to be used by XPathNavigator.

傳回

XPathNavigator

可處理 XPath 查詢的 XPathNavigatorAn XPathNavigator that can process XPath queries.

備註

您無法使用此方法所傳回的 XPathNavigator 來編輯 XML 樹狀結構。You cannot edit the XML tree using the XPathNavigator that is returned by this method. CanEdit 屬性會傳回 falseThe CanEdit property returns false.

您無法建立 XDocumentType 節點的 XPathNavigatorYou cannot create an XPathNavigator for a XDocumentType node. 檔案類型不會參與 XPath 資料模型。Document types do not participate in the XPath data model.

命名空間宣告由左至右回報。Namespace declarations are reported from left to right. 相反地,針對 XmlDocument 的命名空間會從右至左回報。In contrast, for XmlDocument namespaces are reported from right to left. 這是一致的行為,因為不會在 XPath 資料模型中排序命名空間宣告。This is conformant behavior because namespace declarations are not ordered in the XPath data model.

這個方法所傳回的導覽器不支援方法 MoveToIdThe method MoveToId is not supported for navigators that are returned by this method.

如果您使用 XmlNameTable 搭配這個方法來建立 XPathNavigator,則在評估 XPath 運算式時,將可獲得更好的效能。If you use an XmlNameTable with this method to create the XPathNavigator, you will get better performance when evaluating XPath expressions.

適用於