死灵术。
您可以创建自己的 XmlTextWriter 并覆盖 WriteStartDocument。
例子:
public class XmlTextWriterIndentedStandaloneNo : System.Xml.XmlTextWriter
{
public bool bStandAlone = false;
public bool bWriteStartDocument = true;
public bool bOmitEncodingAndStandAlone = true;
public XmlTextWriterIndentedStandaloneNo(System.IO.TextWriter w)
: base(w)
{
Formatting = System.Xml.Formatting.Indented;
} // End Constructor
public XmlTextWriterIndentedStandaloneNo(string strFileName, System.Text.Encoding teEncoding)
: base(strFileName, teEncoding)
{
Formatting = System.Xml.Formatting.Indented;
} // End Constructor
public XmlTextWriterIndentedStandaloneNo(System.IO.Stream w, System.Text.Encoding teEncoding)
: base(w, teEncoding)
{
Formatting = System.Xml.Formatting.Indented;
} // End Constructor
public override void WriteStartDocument(bool standalone)
{
if (bWriteStartDocument)
{
if (bOmitEncodingAndStandAlone)
{
this.WriteProcessingInstruction("xml", "version='1.0'");
return;
} // End if (bOmitEncodingAndStandAlone)
base.WriteStartDocument(bStandAlone);
}
} // End Sub WriteStartDocument
public override void WriteStartDocument()
{
// Suppress by ommitting WriteStartDocument
if (bWriteStartDocument)
{
if (bOmitEncodingAndStandAlone)
{
this.WriteProcessingInstruction("xml", "version='1.0'");
return;
} // End if (bOmitEncodingAndStandAlone)
base.WriteStartDocument(bStandAlone);
// False: Standalone="no"
} // End if (bWriteStartDocument)
} // End Sub WriteStartDocument
} // End Class XmlTextWriterIndentedStandaloneNo
VB.NET
Public Class XmlTextWriterIndentedStandaloneNo
Inherits System.Xml.XmlTextWriter
Public bStandAlone As Boolean = False
Public bWriteStartDocument As Boolean = True
Public bOmitEncodingAndStandAlone As Boolean = True
Public Sub New(w As System.IO.TextWriter)
MyBase.New(w)
Formatting = System.Xml.Formatting.Indented
End Sub
' End Constructor
Public Sub New(strFileName As String, teEncoding As System.Text.Encoding)
MyBase.New(strFileName, teEncoding)
Formatting = System.Xml.Formatting.Indented
End Sub
' End Constructor
Public Sub New(w As System.IO.Stream, teEncoding As System.Text.Encoding)
MyBase.New(w, teEncoding)
Formatting = System.Xml.Formatting.Indented
End Sub
' End Constructor
Public Overrides Sub WriteStartDocument(standalone As Boolean)
If bOmitEncodingAndStandAlone Then
Me.WriteProcessingInstruction("xml", "version='1.0'")
Return
End If
' End if (bOmitEncodingAndStandAlone)
If bWriteStartDocument Then
MyBase.WriteStartDocument(bStandAlone)
End If
End Sub
' End Sub WriteStartDocument
Public Overrides Sub WriteStartDocument()
If bOmitEncodingAndStandAlone Then
Me.WriteProcessingInstruction("xml", "version='1.0'")
Return
End If
' End if (bOmitEncodingAndStandAlone)
' Suppress by ommitting WriteStartDocument
If bWriteStartDocument Then
' False: Standalone="no"
MyBase.WriteStartDocument(bStandAlone)
End If
' End if (bWriteStartDocument)
End Sub
' End Sub WriteStartDocument
End Class
' End Class XmlTextWriterIndentedStandaloneNo
使用示例:
//using (System.Xml.XmlTextWriter wr = new System.Xml.XmlTextWriter(System.IO.Path.Combine(strBasePath, "TestFile.xml"), System.Text.Encoding.UTF8))
//using (System.Xml.XmlWriter wr = System.Xml.XmlWriter.Create(System.IO.Path.Combine(strBasePath, "TestFile.xml"), xwsSettings))
using (System.Xml.XmlWriter wr = new XmlTextWriterIndentedStandaloneNo(System.IO.Path.Combine(strBasePath, "TestFile.xml"), System.Text.Encoding.UTF8))
{
//wr.Formatting = System.Xml.Formatting.None; // here's the trick !
xdoc.Save(wr);
wr.Flush();
wr.Close();
} // End Using wr