我有,看起來像..添加XML聲明字符串XML
<Root>
<Data>Nack</Data>
<Data>Nelly</Data>
</Root>
我想補充"<?xml version=\"1.0\"?>"
這個字符串的一些XML數據。然後將xml保存爲一個字符串。
我嘗試了一些東西..
這種斷裂,失去原有的XML字符串
myOriginalXml="<?xml version=\"1.0\"?>" + myOriginalXml;
,這並不做任何事,只是保持原始的XML數據,不附加任何聲明。
XmlDocument doc = new XmlDocument();
doc.LoadXml(myOriginalXml);
XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", "UTF-8","no");
StringWriter sw = new StringWriter();
XmlTextWriter tx = new XmlTextWriter(sw);
doc.WriteTo(tx);
string xmlString = sw.ToString();
這也似乎不會有任何效果..
XmlDocument doc = new XmlDocument();
doc.LoadXml(myOriginalXml);
XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", "UTF-8", "no");
MemoryStream xmlStream = new MemoryStream();
doc.Save(xmlStream);
xmlStream.Flush();
xmlStream.Position = 0;
doc.Load(xmlStream);
StringWriter sw = new StringWriter();
XmlTextWriter tx = new XmlTextWriter(sw);
doc.WriteTo(tx);
string xmlString = sw.ToString();
的可能重複[我的XDeclaration?](http://stackoverflow.com/questions/6269881/where-is-my-xdeclaration) –
也許我的代碼是錯誤的,但保存文件不工作 –