XmlTextReader忽略DtdProcessing标志

时间:2016-12-07 19:27:28

标签: c# xml dtd xmldocument xmltextreader

我正在尝试使用XmlTextReader加载xml内容但由于某种原因,XmlTextReader在处理Xml时忽略了DtdProcessing标志。如果我使用XmlReader,DtdProcessing标志工作正常。 XmlReader的问题在于它会自动将\ r \ n \到#n标准化为我在输出字符串中不需要的内容。

这是我的代码段:

XmlDocument xmlDocument = new XmlDocument();

string contents = @"<?xml version='1.0' encoding='ISO-8859-1' standalone='yes'?>
    <!DOCTYPE content [<!ENTITY ouml '&#246;'>]>
    <content>Test &ouml; Test

    Test</content>";

byte[] byteArray = Encoding.UTF8.GetBytes(contents);
MemoryStream stream = new MemoryStream(byteArray);

//XmlReaderSettings settings = new XmlReaderSettings();
//settings.DtdProcessing = DtdProcessing.Parse;
//settings.IgnoreWhitespace = false;
//XmlReader reader = XmlReader.Create(stream, settings);
//xmlDocument.Load(reader);

XmlTextReader reader = new XmlTextReader(stream);
reader.DtdProcessing = DtdProcessing.Parse;
xmlDocument.Load(reader);

Console.WriteLine(xmlDocument.OuterXml);

输出我从上面的处理中获得:

"<?xml version=\"1.0\" encoding=\"ISO-8859-1\" standalone=\"yes\"?><!DOCTYPE content[<!ENTITY ouml '&#246;'>]><content>Test &ouml; Test\r\n\r\n    Test</content>"

相反,我想要处理DTD的输出字符串:

"<?xml version=\"1.0\" encoding=\"ISO-8859-1\" standalone=\"yes\"?><!DOCTYPE content[<!ENTITY ouml '&#246;'>]><content>Test ö Test\r\n\r\n    Test</content>"

1 个答案:

答案 0 :(得分:0)

代码看起来像这样

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Xml;
using System.Xml.Linq;
using System.IO;


namespace ConsoleApplication31
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XmlDocument xmlDocument = new XmlDocument();
            try
            {
                string contents = @"<?xml version='1.0' encoding='ISO-8859-1' standalone='yes'?>
                <!DOCTYPE content [<!ENTITY ouml '&#246;'>]>
                <content>Test &ouml; Test
                Test</content>";

                MemoryStream stream = new MemoryStream();
                XmlTextWriter writer = new XmlTextWriter(stream, Encoding.GetEncoding("ISO-8859-1"));
                writer.WriteString(contents);
                writer.Flush();

                byte[] bytes = new byte[stream.Length];
                stream.Position = 0;
                stream.Read(bytes, 0, (int)stream.Length);
                Console.WriteLine(Encoding.GetEncoding("ISO-8859-1").GetString(bytes));
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }



        }
    }

}