如何使XMLDOMDocument包含XML声明? [英] How to make XMLDOMDocument include the XML Declaration?

查看:139
本文介绍了如何使XMLDOMDocument包含XML声明?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当XMLDOMDocument保存自身时,如何使它包含 XML声明,例如:

When an XMLDOMDocument saves itself, how can i get it to include the XML Declaration, e.g.:


  • <?xml版本= 1.0编码= UTF-8吗?>

  • <?xml version = 1.0 encoding = UTF-16?>

  • <?xml version = 1.0 encoding = UCS-2?>

  • <?xml version = 1.0 encoding = UCS- 4?>

  • <?xml version = 1.0 encoding = ISO-10646-UCS-2吗? >

  • <?xml version = 1.0 encoding = UNICODE-1-1-UTF-8?> ;

  • <?xml version = 1.0 encoding = UNICODE-2-0-UTF-16?>

  • <?xml version = 1.0 encoding = UNICODE-2-0-UTF-8?>

  • <?xml version = 1.0 encoding = US-ASCII?>

  • <?xml version = 1.0 encoding = ISO-8859-1吗?>

  • <?xml version = 1.0 encoding = WINDOWS-1250?>

  • <?xml version="1.0" encoding="UTF-8" ?>
  • <?xml version="1.0" encoding="UTF-16" ?>
  • <?xml version="1.0" encoding="UCS-2" ?>
  • <?xml version="1.0" encoding="UCS-4" ?>
  • <?xml version="1.0" encoding="ISO-10646-UCS-2" ?>
  • <?xml version="1.0" encoding="UNICODE-1-1-UTF-8" ?>
  • <?xml version="1.0" encoding="UNICODE-2-0-UTF-16" ?>
  • <?xml version="1.0" encoding="UNICODE-2-0-UTF-8" ?>
  • <?xml version="1.0" encoding="US-ASCII" ?>
  • <?xml version="1.0" encoding="ISO-8859-1" ?>
  • <?xml version="1.0" encoding="WINDOWS-1250" ?>

正在内存中创建XMLDOMDomcument对象(即

The XMLDOMDomcument object is being created in memory (i.e. the xml is not being loaded from some outside source):

{
   IXMLDOMDocument2 doc = new DOMDocument60();

   //add nodes to the doc
   ...

   doc.Save(saveTarget);
}

没有xml声明,您只能获取主体xml,例如:

Without the xml declaration you only get the body xml, e.g.:

<Customer>
   ...
</Customer>

而不是完整的XML文档:

rather than the full XML document:

<?xml version="1.0" encoding="US-ASCII" ?>
<Customer>
   ...
</Customer>



问题2



我如何控制对XMLDOMDocument进行编码会在保存到流中时使用?

Question 2

How can i control the encoding the XMLDOMDocument will use when it saves to a stream?

推荐答案

您需要使用MXXMLWriter60,而不是直接保存它。抱歉,我没有C#示例,但是这里是VB.Net的等效版本。有关详细信息,请参见 IMXWriter

You need to use a MXXMLWriter60, instead of saving it directly. Sorry I don't have a C# example, but here is the VB.Net equivalent. See IMXWriter for details.

' Create and load a DOMDocument object.

Dim xmlDoc As New DOMDocument60
xmlDoc.loadXML("<doc><one>test1</one><two>test2</two></doc>")

' Set properties on the XML writer - including BOM, XML declaration and encoding

Dim wrt As New MXXMLWriter60
wrt.byteOrderMark = True
wrt.omitXMLDeclaration = False
wrt.encoding = "US-ASCII"
wrt.indent = True

' Set the XML writer to the SAX content handler.

Dim rdr As New SAXXMLReader60
Set rdr.contentHandler = wrt
Set rdr.dtdHandler = wrt
Set rdr.errorHandler = wrt
rdr.putProperty "http://xml.org/sax/properties/lexical-handler", wrt
rdr.putProperty "http://xml.org/sax/properties/declaration-handler", wrt

' Now pass the DOM through the SAX handler, and it will call the writer

rdr.parse xmlDoc

' Let the writer do its thing

Dim iFileNo As Integer
iFileNo = FreeFile
Open App.Path + "\saved.xml" For Output As #iFileNo
Print #iFileNo, wrt.output
Close #iFileNo

这篇关于如何使XMLDOMDocument包含XML声明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆