
WriteAttributeString 方法写出属性和命名空间声明
WriteAttributeString 方法有两个不同的任务。 一个任务是写出属性并将其与用户定义的命名空间前缀关联。 第二个任务是生成命名空间声明。 如果写出属性并且 localname 参数为 xmlns,则该方法被视为是创建命名空间声明。
在下面的代码示例中,WriteAttributeString 方法用于在元素内写出属性。
'Write the genre attribute.
writer.WriteAttributeString("genre", "novel")
'Write the ISBN attribute.
writer.WriteAttributeString("ISBN", "1-8630-014")
//Write the genre attribute.
writer.WriteAttributeString("genre", "novel");
//Write the ISBN attribute.
writer.WriteAttributeString("ISBN", "1-8630-014");
WriteAttributeString 还根据找到的内容转义属性的文本内容。 如果使用双引号,则 XmlTextWriter 在属性值的文本内容中用 " 进行转义。 如果使用单引号,则用 ' 转义属性值的文本内容。
为了生成命名空间声明,提供一个重载的 WriteAttributeString 方法,允许应用程序定义命名空间声明。 下面的代码示例创建两个默认命名空间。 第一个声明将所有没有前缀的元素绑定到第一个命名空间声明,而任何用“po”前缀声明的元素则被绑定到第二个命名空间声明。
' Write the default namespace, identified as xmlns with no prefix
writer.WriteAttributeString("xmlns", Nothing, "http://www.w3.org/2000/10/XMLSchema")
' Write a namespace for the purchase order with a prefix of "po"
writer.WriteAttributeString("xmlns", "po", Nothing, "http://contoso.com/po")
// Write the default namespace, identified as xmlns with no prefix
writer.WriteAttributeString("xmlns", null, "http://www.w3.org/2000/10/XMLSchema");
// Write a namespace for the purchase order with a prefix of "po"
writer.WriteAttributeString("xmlns", "po", null, "http://contoso.com/po");