XmlTextWriter 中的属性命名空间前缀

更新:November 2007

处理 XmlTextWriter 的属性的命名空间前缀有多种方式: WriteAttributes、WriteAttributeStringWriteStartAttribute

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

处理命名空间前缀的另一种方法是,让应用程序使用将命名空间作为前缀的一个 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>

如果属性具有关联的命名空间 URI,则根据 XML 建议中万维网联合会 (W3C) 命名空间中的命名空间默认设置,属性还必须具有前缀。 下面的代码示例显示在用 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