1

我有一个包含实体的 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 &#x00AD;Salander-O&#x2019;Reilly .</para>
<para>Since that time, the New York state artist consignment statute&#x2014;which,      </para>
 <para>The of the artist&#x2019;s work; the amount o and the like. </para>
 <para>But firss of  &#x201C;dealer&#x201D; and &#x201C;gallery&#x201D; interchangeably.</para>
 <itemizedlist type="&#x2022;">
 <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();
            }
        }
4

0 回答 0