XmlWriter.WriteElementString 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
写入包含字符串值的元素。
重载
| 名称 | 说明 |
|---|---|
| WriteElementString(String, String) |
写入具有指定本地名称和值的元素。 |
| WriteElementString(String, String, String) |
写入具有指定本地名称、命名空间 URI 和值的元素。 |
| WriteElementString(String, String, String, String) |
写入具有指定前缀、本地名称、命名空间 URI 和值的元素。 |
注解
有关此方法的异步版本,请参阅 WriteElementStringAsync。
WriteElementString(String, String)
- Source:
- XmlWriter.cs
- Source:
- XmlWriter.cs
- Source:
- XmlWriter.cs
- Source:
- XmlWriter.cs
- Source:
- XmlWriter.cs
写入具有指定本地名称和值的元素。
public:
void WriteElementString(System::String ^ localName, System::String ^ value);
public void WriteElementString(string localName, string value);
public void WriteElementString(string localName, string? value);
member this.WriteElementString : string * string -> unit
Public Sub WriteElementString (localName As String, value As String)
参数
- localName
- String
元素的本地名称。
- value
- String
元素的值。
例外
缓冲区中有一个字符是有效的 XML 字符,但对输出编码无效。 例如,如果输出编码为 ASCII,则只应对元素和属性名称使用介于 0 到 127 范围内的字符。 无效字符可能位于此方法的参数中,也可能是在写入缓冲区的以前方法的参数中。 此类字符在可能的情况下由字符实体引用进行转义(例如,文本节点或属性值)。 但是,元素和属性名称、注释、处理指令或 CDATA 节中不允许字符实体引用。
在上一个异步操作完成之前调用了一个 XmlWriter 方法。 在这种情况下, InvalidOperationException 会引发消息“正在进行异步操作”。
示例
以下示例使用多个写入方法创建 XML 片段。
using System;
using System.IO;
using System.Xml;
public class Sample
{
private const string m_Document = "sampledata.xml";
public static void Main() {
XmlWriter writer = null;
try {
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
writer = XmlWriter.Create (m_Document, settings);
writer.WriteComment("sample XML fragment");
// Write an element (this one is the root).
writer.WriteStartElement("book");
// Write the namespace declaration.
writer.WriteAttributeString("xmlns", "bk", null, "urn:samples");
// Write the genre attribute.
writer.WriteAttributeString("genre", "novel");
// Write the title.
writer.WriteStartElement("title");
writer.WriteString("The Handmaid's Tale");
writer.WriteEndElement();
// Write the price.
writer.WriteElementString("price", "19.95");
// Lookup the prefix and write the ISBN element.
string prefix = writer.LookupPrefix("urn:samples");
writer.WriteStartElement(prefix, "ISBN", "urn:samples");
writer.WriteString("1-861003-78");
writer.WriteEndElement();
// Write the style element (shows a different way to handle prefixes).
writer.WriteElementString("style", "urn:samples", "hardcover");
// Write the close tag for the root element.
writer.WriteEndElement();
// Write the XML to file and close the writer.
writer.Flush();
writer.Close();
}
finally {
if (writer != null)
writer.Close();
}
}
}
Option Strict
Option Explicit
Imports System.IO
Imports System.Xml
Public Class Sample
Private Shared m_Document As String = "sampledata.xml"
Public Shared Sub Main()
Dim writer As XmlWriter = Nothing
Try
Dim settings As XmlWriterSettings = new XmlWriterSettings()
settings.Indent = true
writer = XmlWriter.Create (m_Document, settings)
writer.WriteComment("sample XML fragment")
' Write an element (this one is the root).
writer.WriteStartElement("book")
' Write the namespace declaration.
writer.WriteAttributeString("xmlns", "bk", Nothing, "urn:samples")
' Write the genre attribute.
writer.WriteAttributeString("genre", "novel")
' Write the title.
writer.WriteStartElement("title")
writer.WriteString("The Handmaid's Tale")
writer.WriteEndElement()
' Write the price.
writer.WriteElementString("price", "19.95")
' Lookup the prefix and write the ISBN element.
Dim prefix As String = writer.LookupPrefix("urn:samples")
writer.WriteStartElement(prefix, "ISBN", "urn:samples")
writer.WriteString("1-861003-78")
writer.WriteEndElement()
' Write the style element (shows a different way to handle prefixes).
writer.WriteElementString("style", "urn:samples", "hardcover")
' Write the close tag for the root element.
writer.WriteEndElement()
' Write the XML to file and close the writer.
writer.Flush()
writer.Close()
Finally
If Not (writer Is Nothing) Then
writer.Close()
End If
End Try
End Sub
End Class
注解
有关此方法的异步版本,请参阅 WriteElementStringAsync。
适用于
WriteElementString(String, String, String)
- Source:
- XmlWriter.cs
- Source:
- XmlWriter.cs
- Source:
- XmlWriter.cs
- Source:
- XmlWriter.cs
- Source:
- XmlWriter.cs
写入具有指定本地名称、命名空间 URI 和值的元素。
public:
void WriteElementString(System::String ^ localName, System::String ^ ns, System::String ^ value);
public void WriteElementString(string localName, string ns, string value);
public void WriteElementString(string localName, string? ns, string? value);
member this.WriteElementString : string * string * string -> unit
Public Sub WriteElementString (localName As String, ns As String, value As String)
参数
- localName
- String
元素的本地名称。
- ns
- String
要与元素关联的命名空间 URI。
- value
- String
元素的值。
例外
缓冲区中有一个字符是有效的 XML 字符,但对输出编码无效。 例如,如果输出编码为 ASCII,则只应对元素和属性名称使用介于 0 到 127 范围内的字符。 无效字符可能位于此方法的参数中,也可能是在写入缓冲区的以前方法的参数中。 此类字符在可能的情况下由字符实体引用进行转义(例如,文本节点或属性值)。 但是,元素和属性名称、注释、处理指令或 CDATA 节中不允许字符实体引用。
在上一个异步操作完成之前调用了一个 XmlWriter 方法。 在这种情况下, InvalidOperationException 会引发消息“正在进行异步操作”。
示例
以下示例使用多个写入方法创建 XML 片段。
using System;
using System.IO;
using System.Xml;
public class Sample
{
private const string m_Document = "sampledata.xml";
public static void Main() {
XmlWriter writer = null;
try {
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
writer = XmlWriter.Create (m_Document, settings);
writer.WriteComment("sample XML fragment");
// Write an element (this one is the root).
writer.WriteStartElement("book");
// Write the namespace declaration.
writer.WriteAttributeString("xmlns", "bk", null, "urn:samples");
// Write the genre attribute.
writer.WriteAttributeString("genre", "novel");
// Write the title.
writer.WriteStartElement("title");
writer.WriteString("The Handmaid's Tale");
writer.WriteEndElement();
// Write the price.
writer.WriteElementString("price", "19.95");
// Lookup the prefix and write the ISBN element.
string prefix = writer.LookupPrefix("urn:samples");
writer.WriteStartElement(prefix, "ISBN", "urn:samples");
writer.WriteString("1-861003-78");
writer.WriteEndElement();
// Write the style element (shows a different way to handle prefixes).
writer.WriteElementString("style", "urn:samples", "hardcover");
// Write the close tag for the root element.
writer.WriteEndElement();
// Write the XML to file and close the writer.
writer.Flush();
writer.Close();
}
finally {
if (writer != null)
writer.Close();
}
}
}
Option Strict
Option Explicit
Imports System.IO
Imports System.Xml
Public Class Sample
Private Shared m_Document As String = "sampledata.xml"
Public Shared Sub Main()
Dim writer As XmlWriter = Nothing
Try
Dim settings As XmlWriterSettings = new XmlWriterSettings()
settings.Indent = true
writer = XmlWriter.Create (m_Document, settings)
writer.WriteComment("sample XML fragment")
' Write an element (this one is the root).
writer.WriteStartElement("book")
' Write the namespace declaration.
writer.WriteAttributeString("xmlns", "bk", Nothing, "urn:samples")
' Write the genre attribute.
writer.WriteAttributeString("genre", "novel")
' Write the title.
writer.WriteStartElement("title")
writer.WriteString("The Handmaid's Tale")
writer.WriteEndElement()
' Write the price.
writer.WriteElementString("price", "19.95")
' Lookup the prefix and write the ISBN element.
Dim prefix As String = writer.LookupPrefix("urn:samples")
writer.WriteStartElement(prefix, "ISBN", "urn:samples")
writer.WriteString("1-861003-78")
writer.WriteEndElement()
' Write the style element (shows a different way to handle prefixes).
writer.WriteElementString("style", "urn:samples", "hardcover")
' Write the close tag for the root element.
writer.WriteEndElement()
' Write the XML to file and close the writer.
writer.Flush()
writer.Close()
Finally
If Not (writer Is Nothing) Then
writer.Close()
End If
End Try
End Sub
End Class
注解
有关此方法的异步版本,请参阅 WriteElementStringAsync。
适用于
WriteElementString(String, String, String, String)
- Source:
- XmlWriter.cs
- Source:
- XmlWriter.cs
- Source:
- XmlWriter.cs
- Source:
- XmlWriter.cs
- Source:
- XmlWriter.cs
写入具有指定前缀、本地名称、命名空间 URI 和值的元素。
public:
void WriteElementString(System::String ^ prefix, System::String ^ localName, System::String ^ ns, System::String ^ value);
public void WriteElementString(string prefix, string localName, string ns, string value);
public void WriteElementString(string? prefix, string localName, string? ns, string? value);
member this.WriteElementString : string * string * string * string -> unit
Public Sub WriteElementString (prefix As String, localName As String, ns As String, value As String)
参数
- prefix
- String
元素的前缀。
- localName
- String
元素的本地名称。
- ns
- String
元素的命名空间 URI。
- value
- String
元素的值。
例外
缓冲区中有一个字符是有效的 XML 字符,但对输出编码无效。 例如,如果输出编码为 ASCII,则只应对元素和属性名称使用介于 0 到 127 范围内的字符。 无效字符可能位于此方法的参数中,也可能是在写入缓冲区的以前方法的参数中。 此类字符在可能的情况下由字符实体引用进行转义(例如,文本节点或属性值)。 但是,元素和属性名称、注释、处理指令或 CDATA 节中不允许字符实体引用。
在上一个异步操作完成之前调用了一个 XmlWriter 方法。 在这种情况下, InvalidOperationException 会引发消息“正在进行异步操作”。
注解
有关此方法的异步版本,请参阅 WriteElementStringAsync。