加载xml文档并保存时,Xml实体将被删除

时间:2013-01-25 06:54:25

标签: c# xml

我有一个包含实体的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>

我想把put也和上面的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();
            }
        }

0 个答案:

没有答案