如何使XMLDOMDocument包含XML声明?
当
XMLDOMDocument自行保存时,如何让它包含XML声明,例如:
><?xml version =“1.0”encoding =“UTF-8”?> XMLDOMDomcument对象正在内存中创建(即xml不是从某些外部源加载的): { IXMLDOMDocument2 doc = new DOMDocument60(); //add nodes to the doc ... doc.Save(saveTarget); } 如果没有xml声明,你只能得到正文xml,例如: <Customer> ... </Customer> 而不是完整的XML文档: <?xml version="1.0" encoding="US-ASCII" ?> <Customer> ... </Customer> 问题2 如何控制encoding the XMLDOMDocument将在保存到流时使用?
您需要使用MXXMLWriter60,而不是直接保存它.对不起,我没有C#示例,但这里是VB.Net的等价物.有关详情,请参见
IMXWriter.
' 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 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |