DOM方式を利用してネームスペースを指定してXMLファイルを出力するコードを紹介します。
概要
こちらの記事ではXMLDocumentを用いて、XMLファイルを出力する方法を紹介しました。今回は、XMLDocumentを用い、ネームスペースを指定したXMLファイルを書き出す方法を紹介します。
コード
private void button9_Click(object sender, EventArgs e)
{
XmlDocument xmlDocument = new XmlDocument();
XmlElement elem = xmlDocument.CreateElement(
"xns", "root", "http://www.ipentec.com/xmlns/sample/data01");
xmlDocument.AppendChild(elem);
//1つめのitemノード
XmlElement item_elem = xmlDocument.CreateElement(
"xns", "item", "http://www.ipentec.com/xmlns/sample/data01");
item_elem.SetAttribute("code", "http://www.ipentec.com/xmlns/sample/data01", "CD-C-032");
elem.AppendChild(item_elem);
XmlElement name_elem = xmlDocument.CreateElement(
"xns:name", "http://www.ipentec.com/xmlns/sample/data01");
item_elem.AppendChild(name_elem);
XmlNode name_node = xmlDocument.CreateNode(XmlNodeType.Text, "", "");
name_node.Value = "Penguin Cookie";
name_elem.AppendChild(name_node);
XmlElement price_elem = xmlDocument.CreateElement(
"xns", "price", "http://www.ipentec.com/xmlns/sample/data01");
item_elem.AppendChild(price_elem);
XmlNode price_node = xmlDocument.CreateNode(XmlNodeType.Text, "", "");
price_node.Value = "520";
price_elem.AppendChild(price_node);
//2つめのitemノード
item_elem = xmlDocument.CreateElement(
"xns:item", "http://www.ipentec.com/xmlns/sample/data01");
item_elem.SetAttribute("code", "http://www.ipentec.com/xmlns/sample/data01", "CD-C-018");
elem.AppendChild(item_elem);
name_node = xmlDocument.CreateNode(
XmlNodeType.Element, "xns:name", "http://www.ipentec.com/xmlns/sample/data01");
name_node.InnerText = "Whale Candy";
item_elem.AppendChild(name_node);
price_node = xmlDocument.CreateNode(
XmlNodeType.Element, "xns", "price", "http://www.ipentec.com/xmlns/sample/data01");
price_node.InnerText = "185";
item_elem.AppendChild(price_node);
//
//3つめのitemノード
item_elem = xmlDocument.CreateElement(
"xns", "item", "http://www.ipentec.com/xmlns/sample/data01");
item_elem.SetAttribute("code", "http://www.ipentec.com/xmlns/sample/data01", "CD-C-042");
elem.AppendChild(item_elem);
name_elem = xmlDocument.CreateElement(
"xns", "name", "http://www.ipentec.com/xmlns/sample/data01");
name_elem.InnerText = "Dorphin Cookie";
item_elem.AppendChild(name_elem);
price_elem = xmlDocument.CreateElement(
"xns:price", "http://www.ipentec.com/xmlns/sample/data01");
price_elem.InnerText = "220";
item_elem.AppendChild(price_elem);
//
xmlDocument.Save(textBox3.Text);
textBox4.Text += "Write Complete!";
}
解説
XMLDocumentクラスのインスタンスを作成します。
XmlDocument xmlDocument = new XmlDocument();
ノードとなるXmlElementクラスのインスタンスを作成します。XMLの名前空間を指定する場合には、CreateElement()メソッドの引数にXML名前空間のプリフィックスとXMLネームスペースのURIを渡します。
XmlElement elem = xmlDocument.CreateElement(
"xns", "root", "http://www.ipentec.com/xmlns/sample/data01");
AppendChlidメソッドで子要素を追加します。ここは名前空間を指定しない場合と同様です。
xmlDocument.AppendChild(elem);
子ノードの作成時も同様にCreateElementメソッドの引数に名前空間のプリフィックスとURIを指定します。属性値の作成をするSetAttribute()メソッドの場合も同様に名前空間のURIを引数に渡します。
XmlElement item_elem = xmlDocument.CreateElement(
"xns", "item", "http://www.ipentec.com/xmlns/sample/data01");
item_elem.SetAttribute("code", "http://www.ipentec.com/xmlns/sample/data01", "CD-C-032");
elem.AppendChild(item_elem);
CreateElement()メソッドでは名前空間プリフィックスとURIを分けて与える引数の与え方以外に、名前空間プリフィックスを含めた名前とURIを引数に渡すCreateElement()メソッドの呼び出し方法も用意されています。
XmlElement name_elem = xmlDocument.CreateElement(
"xns:name", "http://www.ipentec.com/xmlns/sample/data01");
item_elem.AppendChild(name_elem);
2つ目、3つ目のitemノードも同じ要領で作成します。
出力結果
出力結果は以下になります。それぞれのXmlElementにURIを指定していましたが、出力結果では省略されプリフィックス名のみがノードの名称に付加されていることがわかります。
<xns:root xmlns:xns="http://www.ipentec.com/xmlns/sample/data01">
<xns:item xns:code="CD-C-032">
<xns:name>Penguin Cookie</xns:name>
<xns:price>520</xns:price>
</xns:item>
<xns:item xns:code="CD-C-018">
<xns:name>Whale Candy</xns:name>
<xns:price>185</xns:price>
</xns:item>
<xns:item xns:code="CD-C-042">
<xns:name>Dorphin Cookie</xns:name>
<xns:price>220</xns:price>
</xns:item>
</xns:root>
著者
iPentecのメインプログラマー
C#, ASP.NET の開発がメイン、少し前まではDelphiを愛用
最終更新日: 2024-01-07
作成日: 2011-01-16