共用方式為


XmlTextWriter 中的屬性命名空間前置詞

有幾種方法可以為含有 XmlTextWriter 的屬性處理命名空間前置詞:WriteAttributes、WriteAttributeStringWriteStartAttribute

WriteAttributes

如果目前的位置是項目節點,則 WriteAttributes 方法會寫入 XmlReader 中的目前位置上所找到的所有屬性。 若目前位置設定於屬性上,則會傳回該屬性與項目中其餘的屬性。 若位置設定於 XmlDeclaration NodeType 上,則會寫出宣告的所有屬性。 其餘 NodeTypes 皆不會執行作業。 若 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

處理命名空間前置詞的另一個方法是,讓應用程式使用將命名空間當做前置詞的 WriteAttributeString 方法之一。 其中一個 WriteAttributeString 方法會將屬性名稱、屬性值和命名空間做為引數,並且用指定的值寫出屬性,然後將它與命名空間產生關聯。

另一個 WriteAttributeString 方法會使用使用者定義的命名空間前置詞,並在寫入屬性時,將屬性與該前置詞產生關聯。 如需顯示如何使用 WriteAttributeString 方法及使用者定義之命名空間前置詞的範例程式碼,請參閱 WriteAttributeString

WriteStartAttribute

WriteStartAttribute 產生屬性的開頭,並依據所使用的方法,另外將命名空間前置詞或命名空間統一資源識別元 (URI) 作為參數。

下列程式碼範例將第一個輸入引數當做接受命名空間前置詞 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) Namespaces in XML 建議事項中的命名空間預設值,如果屬性有關聯的命名空間 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