我有一个包含实体的 xml 文档。当我加载它并处理并保存时,所有实体都会转换为 UTF 编码。如何获取与输入内容相同的文档。
输入xml是
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE chapter SYSTEM "DTD_v1.5\PLI.dtd">
<chapter num="1" update-date="November 2012" type="chapter">
<page num="3"/>
<title>Artist-Dealer Relations</title>
<para align="left"><content-style font-style="bold">S</content-style>i ­Salander-O’Reilly .</para>
<para>Since that time, the New York state artist consignment statute—which, </para>
<para>The of the artist’s work; the amount o and the like. </para>
<para>But firss of “dealer” and “gallery” interchangeably.</para>
<itemizedlist type="•">
<item><para>To care </para></item>
</itemizedlist>
<chapter>
我想输出也与上面的 xml 相同,我尝试了以下代码
XmlDocument xDoc = new XmlDocument();
string text = string.Empty;
text = ReadFile("38149_Chapter01_Art_Law_XML.xml", enmReadType.None, Encoding.Default);
xDoc.LoadXml(text);
xDoc.Save("38149_Chapter01_Art_Law_XML.xml");
和
string sXmlData = File.ReadAllText("38149_Chapter01_Art_Law_XML.xml", Encoding.Default);
xDoc.LoadXml(sXmlData);
XmlAttribute aa = xDoc.CreateAttribute("Name");
aa.Value = "Sarvesh";
XmlNode snode = xDoc.SelectSingleNode("//chapter");
snode.Attributes.Append(aa);
using (MemoryStream ms = new MemoryStream())
{
XmlWriterSettings settings = new XmlWriterSettings();
settings.Encoding = Encoding.GetEncoding("ISO-8859-1");
XmlWriter xw = XmlWriter.Create(ms, settings);
xDoc.Save(xw);
xw.Close();
using (TextWriter tx = new StreamWriter("38149_Chapter01_Art_Law_XML.xml", false, Encoding.Default))
{
tx.Write(Encoding.UTF8.GetString(ms.ToArray()));
tx.Close();
}
}