MaxCharactersFromEntities

MaxCharactersFromEntities

本文介绍了XmlException:输入文档已超过MaxCharactersFromEntities设置的限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个XML文件,如下所示:

I have an XML file that looks like this:

<!DOCTYPE Root [
<!ELEMENT anEntity (#PCDATA)>
<!ELEMENT 500SuchElementsHere (#PCData)>
<!ENTITY file1 SYSTEM "file1.xml">
...
<!ENTITY file25 SYSTEM "file25.xml">
]>
<Root>
    &file1;
    &file2;
    ...
    &file25;
</Root>

我正在使用XmlDocument这样加载XML文件

I'm loading the XML file using XmlDocument like this

XmlDocument doc = new XmlDocument();
doc.Load("filePath to the above xml file");

加载会抛出标题中提到的异常。我在Windows 7 Ultimate上运行.NET 4.5,VS 2012 Desktop Express。任何帮助是赞赏。谢谢

The load throws the exception mentioned in the title. I'm running .NET 4.5, VS 2012 Desktop Express on Windows 7 Ultimate. Any help is appreciated. Thanks

推荐答案

您需要使用,其中包含设置属性设置为0(或大量将适用于您的场景):

You need to use an XmlReader with the settings property MaxCharactersFromEntities set to 0 (or a large number that will work for your scenario):

        var doc = new XmlDocument();

        using (var stream = new MemoryStream(Encoding.Default.GetBytes(xml)))
        {
            var settings = new XmlReaderSettings();

            // The default is 0, but setting it here allows us to document exactly why we are taking this approach.
            settings.MaxCharactersFromEntities = 0;

            using (var reader = XmlReader.Create(stream, settings))
            {
                doc.Load(reader);
            }
        }

这篇关于XmlException:输入文档已超过MaxCharactersFromEntities设置的限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 04:22