如何获取带有标头(<?xml version =“ 1.0” ......)的XML? [英] How to get XML with header (<?xml version="1.0"...)?
本文介绍了如何获取带有标头(<?xml version =“ 1.0” ......)的XML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
考虑以下创建XML文档并显示它的简单代码。
Consider the following simple code which creates an XML document and displays it.
XmlDocument xml = new XmlDocument();
XmlElement root = xml.CreateElement("root");
xml.AppendChild(root);
XmlComment comment = xml.CreateComment("Comment");
root.AppendChild(comment);
textBox1.Text = xml.OuterXml;
它按预期显示:
<root><!--Comment--></root>
但是不会显示
<?xml version="1.0" encoding="UTF-8"?>
那么我怎么也能得到呢?
So how can I get that as well?
推荐答案
使用 XmlDocument.CreateXmlDeclaration方法:
XmlNode docNode = xml.CreateXmlDeclaration("1.0", "UTF-8", null);
xml.AppendChild(docNode);
注意:请查看该方法的文档,尤其是对于 encoding
参数:此参数的值有特殊要求。
Note: please take a look at the documentation for the method, especially for encoding
parameter: there are special requirements for values of this parameter.
这篇关于如何获取带有标头(<?xml version =“ 1.0” ......)的XML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文