本文的發布號曾為 CHS313188
有關本文的 Microsoft Visual Basic .NET 版本,請參見 308062。
本任務的內容
•概要
•
•建立 XML 檔案
•建立 Visual C# .NET 項目
•參考
概要
本文介紹如何通過指定命名空間首碼:元素名 格式的完全限定元素名在 XmlDocument 對象中選擇節點。
返回頁首
建立 XML 檔案
1.在 Windows 開始菜單上,指向運行,鍵入 notepad.exe,然後單擊確定以開啟記事本。 複製以下代碼並將其粘貼到記事本中:
<?xml version='1.0' encoding='utf-16'?>
<bk:Books xmlns:bk='http://myserver/myschemas/Books'>
<bk:Book>
<bk:Title>Just XML</bk:Title>
</bk:Book>
<bk:Book>
<bk:Title>Professional XML</bk:Title>
</bk:Book>
<bk:Book>
<bk:Title>XML Step by Step</bk:Title>
</bk:Book>
<bk:Book>
<bk:Title>XML By Example</bk:Title>
</bk:Book>
</bk:Books>2.複製以下代碼並將其粘貼到記事本中: 在檔案菜單上,單擊儲存。
3.複製以下代碼並將其粘貼到記事本中: 在另存新檔對話方塊的檔案類型文字框中,鍵入所有檔案。 在檔案名稱文字框中,鍵入 Books.xml,然後單擊確定。
返回頁首
建立 Visual C# .NET 項目
下面的程式碼範例使用以下對象和類:
•XPathNavigator 類: XPathNavigator 基於 XML 路徑語言 (XPath) 資料模型,該類提供對任何資料存放區區實現 XPath 查詢所需要的方法。
•XPathExpression 類: 該類封裝已編譯的 XPath 運算式,在調用 Compile 時會返回該類。 Select、Evaluate 和 Matches 方法使用該類。
•XmlNamespaceManager 類: XmlNamespaceManager 用於解析命名空間,向集合添加命名空間,以及從集合刪除命名空間。 XmlNamespaceManager 還為這些命名空間提供範圍管理。 因為 Books.xml 在代碼中使用下面的“bk”命名空間,所以您必須使用 XmlNamespaceManager。
•XPathNodeIterator 類: 該對象提供對一組選定節點的迭代程式。
若要建立並運行 Visual Basic .NET 項目,請按下列步驟操作:
1.在 Visual C# .NET 中建立一個 Windows 應用程式項目。預設情況下會將 Form1 添加到項目中。
2.將一個 Button 控制項和一個 TextBox 控制項放到 Form1 上。
3.將 TextBox 控制項的 MultiLine 屬性設定為 True。
4.單擊以展開 TextBox 控制項,以便查看四到五行資料。
5.將下面的代碼添加到“代碼”視窗頂部:
using System.Xml;
using System.Xml.XPath;6.若要將 Books.xml file 檔案載入到 XmlDocument 對象中,請將下面的代碼添加到 Button 對象的 Click 事件中。
XmlDocument oxmldoc = new XmlDocument();
oxmldoc.Load(@"c:\Books.xml");7.確保前面代碼中的 Books.xml 路徑指向電腦上的正確路徑。
8.使用 XmlDocument 對象的 CreateNavigator 方法建立 XPathNavigator 對象,以便運行 XPath 查詢:
XPathNavigator oXPathNav;
oXPathNav = oxmldoc.CreateNavigator();9.使用 XPathNavigator 的 Compile 方法建立 XPathExpression 類,然後傳遞 XPath 查詢作為參數:
XPathExpression Expr;
Expr = oXPathNav.Compile("//bk:Book[position()>=2]");10.使用 AddNamespace 方法向 XmlNamespaceManager 對象添加“bk”命名空間:
XmlNamespaceManager(oXPathNav.NameTable);
oxmlNSManager.AddNamespace("bk", "http://myserver/myschemas/Books");11.使用 XPathExpression 的 SetContext 方法將 XPathExpression 上下文設定為 XmlNamespaceManager:
Expr.SetContext(oxmlNSManager);12.若要運行 XPath 查詢並返回選定的節點,請將運算式傳遞給 XPathNodeIterator 的 Select 方法:
XPathNodeIterator iterator = oXPathNav.Select(Expr);
while (iterator.MoveNext())
{
this.textBox1.Text = this.textBox1.Text + "\r\n"+ iterator.Current.Value ;
}13.Button1_Click 事件中的代碼應顯示如下:
XmlDocument oxmldoc = new XmlDocument();
try
{
oxmldoc.Load("c:\\Books.xml");
XPathNavigator oXPathNav;
oXPathNav = oxmldoc.CreateNavigator();
XPathExpression Expr;
Expr = oXPathNav.Compile("//bk:Book[position()>=2]");
XmlNamespaceManager oxmlNSManager = new XmlNamespaceManager(oXPathNav.NameTable);
oxmlNSManager.AddNamespace("bk", "http://myserver/myschemas/Books");
Expr.SetContext(oxmlNSManager);
XPathNodeIterator iterator = oXPathNav.Select(Expr);
while (iterator.MoveNext())
{
this.textBox1.Text = this.textBox1.Text + "\r\n"+ iterator.Current.Value ;
}
oxmlNSManager = null;
oXPathNav = null;
oxmldoc = null;
}
catch (Exception exc)
{
MessageBox.Show(exc.Message);
}14.產生並運行該項目。
15.單擊 Button1。 注意,文字框中將出現一個書籍列表,這些書的位置大於或等於 2。
返回頁首
參考
有關其他資訊,請單擊下面的文章編號,以查看 Microsoft 知識庫中的文章:
280457 PRB: Specifying Fully Qualified Element Names in XPath Queries(在 XPath 查詢中指定完整元素名稱)
本文來自CSDN部落格,轉載請標明出處:http://blog.csdn.net/wukong777/archive/2004/10/09/129415.aspx