XmlTextWriter.XmlSpace 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取表示当前 xml:space
范围的 XmlSpace。
public:
virtual property System::Xml::XmlSpace XmlSpace { System::Xml::XmlSpace get(); };
public override System.Xml.XmlSpace XmlSpace { get; }
member this.XmlSpace : System.Xml.XmlSpace
Public Overrides ReadOnly Property XmlSpace As XmlSpace
属性值
一个表示当前 XmlSpace
范围的 xml:space
。
值 | 含义 |
---|---|
无 | 如果不存在 xml:space 范围,则此为默认值。
|
默认 | 当前范围为 xml:space ="default"。
|
保留 | 当前范围为 xml:space ="preserve"。
|
示例
下面的示例使用 WriteWhitespace
该方法控制文件格式的方式。
#using <System.Xml.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
{
// Create the writer.
XmlTextWriter^ writer = nullptr;
writer = gcnew XmlTextWriter( "ws.html", nullptr );
// Write an element (this one is the root).
writer->WriteStartElement( "p" );
// Write the xml:space attribute.
writer->WriteAttributeString( "xml", "space", nullptr, "preserve" );
// Verify that xml:space is set properly.
if ( writer->XmlSpace == XmlSpace::Preserve )
Console::WriteLine( "xmlspace is correct!" );
// Write out the HTML elements. Insert white space
// between 'something' and 'Big'
writer->WriteString( "something" );
writer->WriteWhitespace( " " );
writer->WriteElementString( "b", "B" );
writer->WriteString( "ig" );
// Write the root end element.
writer->WriteEndElement();
// Write the XML to file and close the writer.
writer->Close();
}
using System;
using System.IO;
using System.Xml;
public class Sample
{
public static void Main()
{
// Create the writer.
XmlTextWriter writer = null;
writer = new XmlTextWriter ("ws.html", null);
// Write an element (this one is the root).
writer.WriteStartElement("p");
// Write the xml:space attribute.
writer.WriteAttributeString("xml", "space", null, "preserve");
// Verify that xml:space is set properly.
if (writer.XmlSpace == XmlSpace.Preserve)
Console.WriteLine("xmlspace is correct!");
// Write out the HTML elements. Insert white space
// between 'something' and 'Big'
writer.WriteString("something");
writer.WriteWhitespace(" ");
writer.WriteElementString("b", "B");
writer.WriteString("ig");
// Write the root end element.
writer.WriteEndElement();
// Write the XML to file and close the writer.
writer.Close();
}
}
Option Strict
Option Explicit
Imports System.IO
Imports System.Xml
Public Class Sample
Public Shared Sub Main()
' Create the writer.
Dim writer As XmlTextWriter = Nothing
writer = New XmlTextWriter("ws.html", Nothing)
' Write an element (this one is the root).
writer.WriteStartElement("p")
' Write the xml:space attribute.
writer.WriteAttributeString("xml", "space", Nothing, "preserve")
' Verify that xml:space is set properly.
If writer.XmlSpace = XmlSpace.Preserve Then
Console.WriteLine("xmlspace is correct!")
End If
' Write out the HTML elements. Insert white space
' between 'something' and 'Big'.
writer.WriteString("something")
writer.WriteWhitespace(" ")
writer.WriteElementString("b", "B")
writer.WriteString("ig")
' Write the root end element.
writer.WriteEndElement()
' Write the XML to file and close the writer.
writer.Close()
End Sub
End Class
注解
备注
从 .NET Framework 2.0 开始,我们建议使用XmlWriter.Create方法和XmlWriterSettings类创建XmlWriter实例,以利用新功能。
此属性允许一个组件找出另一个组件离开编写器的状态。