XmlWriter.WriteElementString 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
寫入包含字串值的元素。
多載
| 名稱 | Description |
|---|---|
| WriteElementString(String, String) |
會寫入一個以指定本地名稱和值的元素。 |
| WriteElementString(String, String, String) |
寫入一個元素,包含指定的本地名稱、命名空間 URI 和值。 |
| WriteElementString(String, String, String, String) |
會寫入一個元素,包含指定的前綴、本地名稱、命名空間 URI 和值。 |
備註
關於此方法的非同步版本,請參見 WriteElementStringAsync。
WriteElementString(String, String)
- 來源:
- XmlWriter.cs
- 來源:
- XmlWriter.cs
- 來源:
- XmlWriter.cs
- 來源:
- XmlWriter.cs
- 來源:
- 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)
- 來源:
- XmlWriter.cs
- 來源:
- XmlWriter.cs
- 來源:
- XmlWriter.cs
- 來源:
- XmlWriter.cs
- 來源:
- 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)
- 來源:
- XmlWriter.cs
- 來源:
- XmlWriter.cs
- 來源:
- XmlWriter.cs
- 來源:
- XmlWriter.cs
- 來源:
- 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。