XPathを使ってネームスペース(名前空間)が指定されているXMLファイルをパースする (XmlDocumentを利用) - C#

ネームスペースが指定されているXMLファイルをXPathを使ってパーシングするコードを紹介します。

概要

XPathを用いる場合は、XmlDocumentクラスのSelectNodesメソッドを用います。SelectNodesメソッドの引数にXPath式を与えることでXPath式に一致するノードをまとめて選択できます。
ネームスペースが指定されている場合はさらに、XmlNamespaceManagerクラスを作成し、XmlNamespaceManagerクラスに選択する対象のネームスペースを追加し、名前空間のプリフィックスと関連付けます。SelectNodesメソッドを呼び出す際に、XmlNamespaceManagerクラスを引数に与えます。また、XPath式に名前空間のプリフィックスを指定します。/名前空間プリフィックス:ノード名/...の形式で指定します。)

プログラム

コード例

private void button4_Click(object sender, EventArgs e)
{
  XmlDocument xmlDocument = new XmlDocument();
  xmlDocument.Load(textBox1.Text);

  XmlNamespaceManager xmlNsManager = new XmlNamespaceManager(xmlDocument.NameTable);
  xmlNsManager.AddNamespace("ns", "http://www.ipentec.com/xmlns/sample/data01");

  XmlNodeList nodeList = xmlDocument.SelectNodes("/ns:root/ns:node/ns:data", xmlNsManager);

  for (int i=0; i < nodeList.Count; i++) {
    textBox2.Text += "NodeType: " + nodeList[i].NodeType + "\r\n";
    textBox2.Text += "Name: " + nodeList[i].Name + "\r\n";
    textBox2.Text += "LocalName: " + nodeList[i].LocalName + "\r\n";
    textBox2.Text += "Value: " + nodeList[i].Value + "\r\n";
    textBox2.Text += "InnerText: " + nodeList[i].InnerText + "\r\n";
    textBox2.Text += "\r\n";
    if (nodeList[i].HasChildNodes == true) {
      for (int j=0; j < nodeList[i].ChildNodes.Count; j++) {
        textBox2.Text += "NodeType: " + nodeList[i].ChildNodes[j].NodeType + "\r\n";
        textBox2.Text += "Name: " + nodeList[i].ChildNodes[j].Name + "\r\n";
        textBox2.Text += "LocalName: " + nodeList[i].ChildNodes[j].LocalName + "\r\n";
        textBox2.Text += "Value: " + nodeList[i].ChildNodes[j].Value + "\r\n";
        textBox2.Text += "InnerText: " + nodeList[i].ChildNodes[j].InnerText + "\r\n";
      }
    }
    textBox2.Text += "\r\n";
  }
}

解説

下記のコードでXmlDocumentの宣言とインスタンスの作成をします。
 XmlDocument xmlDocument = new XmlDocument();

下記コードでLoad()メソッドでXMLファイルを読み込みます。
 xmlDocument.Load(textBox1.Text);

XmlNamespaceManagerクラスを作成します。コンストラクタの引数にXmlDocumentクラスのNameTableプロパティを与えます。
 XmlNamespaceManager xmlNsManager = new XmlNamespaceManager(xmlDocument.NameTable);

XmlNamespaceManagerクラスにネームスペースを追加し、名前空間プリフィックスとネームスペースとを関連付けします。
 xmlNsManager.AddNamespace("ns", "http://www.ipentec.com/xmlns/sample/data01");

XPath書式を用いて複数ノードを選択します。XPath式のノード名に名前空間プリフィックスを指定します。
 XmlNodeList nodeList = xmlDocument.SelectNodes("/ns:root/ns:node/ns:data",xmlNsManager );

選択されたノードの数だけループします。
 for (int i=0; i < nodeList.Count; i++){

ループ内の処理でノードの情報をtextBox2に出力します。
 textBox2.Text += "NodeType: " + nodeList[i].NodeType + "\r\n";
 textBox2.Text += "Name: " + nodeList[i].Name + "\r\n";
 textBox2.Text += "LocalName: " + nodeList[i].LocalName + "\r\n";
 textBox2.Text += "Value: " + nodeList[i].Value + "\r\n";
 textBox2.Text += "InnerText: " + nodeList[i].InnerText + "\r\n";
 textBox2.Text += "\r\n";

ノードに子ノードがあるか確認します。
 if (nodeList[i].HasChildNodes == true){

子ノードがある場合は、子ノードの内容をtextBox2に表示します。
 for (int j=0; j < nodeList[i].ChildNodes.Count; j++) {
   textBox2.Text += "NodeType: " + nodeList[i].ChildNodes[j].NodeType + "\r\n";
   textBox2.Text += "Name: " + nodeList[i].ChildNodes[j].Name + "\r\n";
   textBox2.Text += "LocalName: " + nodeList[i].ChildNodes[j].LocalName + "\r\n";
   textBox2.Text += "Value: " + nodeList[i].ChildNodes[j].Value + "\r\n";
   textBox2.Text += "InnerText: " + nodeList[i].ChildNodes[j].InnerText + "\r\n";
 }

実行結果

入力データ (XMLファイル)

<?xml version="1.0" encoding="UTF-8"?> 
<root xmlns="http://www.ipentec.com/xmlns/sample/data01"> 
  <node>
    <data type="text">データ1です</data> 
  </node>
  <node> 
    <data type="text">データ2です</data> 
  </node>
  <node>
    <data type="text">データ3です</data> 
  </node>
</root>

結果

NodeType: Element
Name: data
LocalName: data
Value: 
InnerText: データ1です

NodeType: Text
Name: #text
LocalName: #text
Value: データ1です
InnerText: データ1です

NodeType: Element
Name: data
LocalName: data
Value: 
InnerText: データ2です

NodeType: Text
Name: #text
LocalName: #text
Value: データ2です
InnerText: データ2です

NodeType: Element
Name: data
LocalName: data
Value: 
InnerText: データ3です

NodeType: Text
Name: #text
LocalName: #text
Value: データ3です
InnerText: データ3です
著者
iPentecのメインプログラマー
C#, ASP.NET の開発がメイン、少し前まではDelphiを愛用
最終更新日: 2024-01-07
作成日: 2011-01-10
iPentec all rights reserverd.