次の方法で共有


XmlTextWriter における属性の名前空間プレフィックス

属性の名前空間プレフィックスを処理する方法には、XmlTextWriter: WriteAttributesWriteAttributeStringWriteStartAttribute という複数の方法があります。

WriteAttributes

現在の位置が要素ノードである場合、WriteAttributes メソッドは、XmlReader の現在の位置にあるすべての属性を書き込みます。 現在の位置が属性に設定されている場合は、その属性と要素内のその他すべての属性が返されます。 位置が XmlDeclaration NodeType に設定されている場合は、その宣言のすべての属性が書き出されます。 その他すべての NodeType では何も行われません。 XmlReader で名前空間プレフィックスが宣言されている場合、WriteAttributes メソッドは属性の他にその名前空間プレフィックスを書き込みます。 WriteAttributes メソッドが属性と名前空間プレフィックスをどのように書き込むかを示すコード サンプルを次に示します。

Dim r As XmlTextReader = CreateTextReaderStr("<ROOT xmlns:p='n' p:a='abc'/>")
r.Read()
r.MoveToAttribute("p:a")

Dim tw As New XmlTextWriter(Console.Out)
tw.WriteStartElement("ROOT")
tw.WriteAttributes(r, False)
tw.WriteEndElement()
XmlTextReader r = CreateTextReaderStr("<ROOT xmlns:p='n' p:a='abc'/>");
r.Read();
r.MoveToAttribute("p:a");

XmlTextWriter tw = new XmlTextWriter(Console.Out);
tw.WriteStartElement("ROOT");
tw.WriteAttributes(r, false);
tw.WriteEndElement();

出力

<ROOT p:a="abc" xmlns:p="n" />

WriteAttributeString

名前空間内のプレフィックスを処理するもう 1 つの方法は、名前空間をプレフィックスとして受け取るいずれかの WriteAttributeString メソッドをアプリケーションで使用する方法です。 WriteAttributeString メソッドの中には、属性名、属性値、および名前空間を引数として受け取り、渡された値を持つ属性を書き出して、それに名前空間を関連付けるものがあります。

ユーザー定義の名前空間プレフィックスを使用し、属性を書き込むときに属性をそのプレフィックスに関連付ける WriteAttributeString メソッドもあります。 WriteAttributeString メソッドおよびユーザー定義の名前空間プレフィックスの使用方法を示すコード サンプルについては、WriteAttributeString に関するトピックを参照してください。

WriteStartAttribute

WriteStartAttribute は、属性の開始を生成し、さらに、使用するメソッドに応じて名前空間プレフィックスまたは名前空間 URI (Uniform Resource Identifier) をパラメーターとして受け取ります。

次のコード サンプルでは、最初の入力用の引数を名前空間プレフィックス bk としていて、そのプレフィックスは LookupPrefix メソッドを呼び出すことによって検索されています。 WriteStartAttribute を使用してプレフィックス、ローカル名、名前空間を設定した後に、WriteString メソッドによって属性に値が与えられます。

' Write an element (this one is the root).
writer.WriteStartElement("bookstore")
' Write the namespace declaration.
writer.WriteAttributeString("xmlns", "bk", Nothing, "urn:samples")
writer.WriteStartElement("book")
' Look up the prefix, and then write the ISBN attribute.
Dim prefix As String = writer.LookupPrefix("urn:samples")
writer.WriteStartAttribute(prefix, "ISBN", "urn:samples")
writer.WriteString("1-861003-78")
writer.WriteEndAttribute()
' Write the style element.
writer.WriteStartElement(prefix, "style", "urn:samples")
writer.WriteString("hardcover")
writer.WriteEndElement()
' Write the end tag for the book element.
writer.WriteEndElement()
'Write the close tag for the root element.
writer.WriteEndElement()
                
// Write an element (this one is the root).
writer.WriteStartElement("bookstore");   
// Write the namespace declaration.
writer.WriteAttributeString("xmlns", "bk", null, "urn:samples");
writer.WriteStartElement("book");
// Look up the prefix, and then write the ISBN attribute.
string prefix = writer.LookupPrefix("urn:samples");
writer.WriteStartAttribute(prefix, "ISBN", "urn:samples");
writer.WriteString("1-861003-78");
writer.WriteEndAttribute();
// Write the style element.
writer.WriteStartElement(prefix, "style", "urn:samples");
writer.WriteString("hardcover");
writer.WriteEndElement();
// Write the end tag for the book element.
writer.WriteEndElement();
// Write the close tag for the root element.
writer.WriteEndElement();

出力

<bookstore xmlns:bk="urn:samples">
  <book bk:ISBN="1-861003-78">
      <bk:style>hardcover</bk:style>
  </book>
</bookstore>

W3C (World Wide Web Consortium) 勧告『Namespaces in XML』の「Namespace Defaulting」によれば、属性に名前空間 URI が関連付けられている場合は、その属性にプレフィックスを持たせる必要があります。 次のコード サンプルでは、WriteAttributeString メソッドを使用して属性を書き出すときにプレフィックスを含む必要があることを示しています。

Dim w As New XmlTextWriter(Console.Out)
w.WriteStartElement("root")
w.WriteAttributeString("order", "urn:1", "123")
w.WriteEndElement()
w.Close()
XmlTextWriter w = new XmlTextWriter(Console.Out);
w.WriteStartElement("root");
w.WriteAttributeString("order","urn:1", "123");
w.WriteEndElement();
w.Close();

出力

<root n1:order="123" xmlns:n1="urn:1"/>

この出力は、root 要素が既定の名前空間 urn:1 に関連付けられている場合にも生成されます。 プレフィックスは n{i} という形式で指定されます。ここで、i は 1 から開始されます。 このインデックスは各要素で 1 から開始されます。 そのため、入れ子になった子要素に対してもプレフィックスを生成する必要がある場合は、n1 を使用します。

次のコード サンプルは、異なる名前空間を持つ複数の属性を書く場合に、名前空間宣言の前に属性が書かれることを示しています。

Dim w As New XmlTextWriter(Console.Out)
w.WriteStartElement("root")
w.WriteAttributeString("order", "urn:1", "123")
w.WriteAttributeString("book", "urn:2", "The Great Escape")
w.WriteEndElement()
w.Close()
XmlTextWriter w = new XmlTextWriter(Console.Out);
w.WriteStartElement("root");
w.WriteAttributeString("order","urn:1", "123");
w.WriteAttributeString("book","urn:2", "The Great Escape");
w.WriteEndElement();
w.Close();

出力

<root n1:order="123" n2:book="The Great Escape" xmlns:n1="urn:1" xmlns:n2="urn:2"/>

参照

参照

XmlTextWriter

XmlTextWriter

XmlWriter

XmlWriter