請考慮以下簡單的代碼,它可以創建一個XML文檔並將其顯示出來。如何使用標題獲取XML(<?xml version =「1.0」...)?
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"?>
所以,我怎麼能拿到呢?
感謝。我認爲這是自動的。 – ispiro 2013-02-12 18:53:34
+1。請注意,預計「Utf-8」與字符串編碼不匹配(請參閱+1 Nicholas Carey答案)。 – 2013-02-12 19:59:55
@AlexeiLevenkov謝謝。但是我用'OuterXml'來使用它。或者我錯過了一些東西,甚至有問題? – ispiro 2013-02-12 21:40:11