Extensions.CreateNavigator 方法

定義

建立 XPathNavigatorXNode

多載

CreateNavigator(XNode)

建立 XPathNavigatorXNode

CreateNavigator(XNode, XmlNameTable)

建立 XPathNavigatorXNodeXmlNameTable 可讓 XPath 運算式處理時更有效率。

備註

您無法使用 XPathNavigator 這個方法傳回的 來編輯 XML 樹狀結構。 CanEdit 屬性會傳回 false

您無法為 XDocumentType 節點建立 XPathNavigator 。 檔案類型不會參與 XPath 資料模型。

命名空間宣告會從左至右報告。 相反地,針對 XmlDocument ,命名空間會從右至左報告。 這是一致的行為,因為命名空間宣告不會在 XPath 資料模型中排序。

MoveToId此方法不支援此方法所傳回的導覽器。

您可以使用這個方法來執行 XSLT 轉換。 您可以建立 XML 樹狀結構、從 XML 樹狀結構建立 XPathNavigator 、建立新檔,以及建立 XmlWriter 將寫入新檔的 。 然後,您可以叫用 (Invoke) XSLT 轉換,將 XPathNavigatorXmlWriter 傳遞至轉換。 轉換成功完成後,系統會使用轉換的結果填入新的 XML 樹狀結構。

若要執行 XSLT 轉換,您可以使用 XmlReaderXPathNavigator 。 這兩種方法有不同的效能特性。 使用 時 XmlReader ,某些轉換的執行速度會更快,而其他 XPathNavigator 轉換在使用 時會更快執行。 如果效能是一個考慮,建議您試驗每個方法,以判斷哪一種方法會在您的情況中表現得更好。

CreateNavigator(XNode)

建立 XPathNavigatorXNode

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

您無法為 XDocumentType 節點建立 XPathNavigator 。 檔案類型不會參與 XPath 資料模型。

命名空間宣告會從左至右報告。 相反地,命名空間 XmlDocument 會從右至左報告。 這是一致的行為,因為命名空間宣告不會在 XPath 資料模型中排序。

MoveToId此方法不支援此方法所傳回的導覽器。

您可以使用這個方法來執行 XSLT 轉換。 您可以建立 XML 樹狀結構、從 XML 樹狀結構建立 XPathNavigator 、建立新檔,以及建立 XmlWriter 將寫入新檔的 。 然後,您可以叫用 XSLT 轉換,並將 傳遞 XPathNavigator XmlWriter 至轉換。 轉換成功完成後,系統會使用轉換的結果填入新的 XML 樹狀結構。

若要執行 XSLT 轉換,您可以使用 XmlReaderXPathNavigator 。 這兩種方法有不同的效能特性。 使用 時 XmlReader ,某些轉換的執行速度會更快,而其他 XPathNavigator 轉換在使用 時會更快執行。 如果效能是一個考慮,建議您試驗每個方法,以判斷哪一種方法會在您的情況中表現得更好。

適用於

CreateNavigator(XNode, XmlNameTable)

建立 XPathNavigatorXNodeXmlNameTable 可讓 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

您無法為 XDocumentType 節點建立 XPathNavigator 。 檔案類型不會參與 XPath 資料模型。

命名空間宣告會從左至右報告。 相反地,命名空間 XmlDocument 會從右至左報告。 這是一致的行為,因為命名空間宣告不會在 XPath 資料模型中排序。

MoveToId此方法不支援此方法所傳回的導覽器。

如果您使用 XmlNameTable 搭配這個方法來建立 XPathNavigator ,則評估 XPath 運算式時,將會獲得更好的效能。

適用於