prevent DTD下载解析XML时prevent、DTD、XML

2023-09-02 11:56:46 作者:小姐,不要離開我

在使用XMLDocument.load方法,我发现,如果该文件是指一个DTD,建立连接到所提供的URI。有没有什么办法prevent这种情况的发生?

When using XmlDocument.Load , I am finding that if the document refers to a DTD, a connection is made to the provided URI. Is there any way to prevent this from happening?

推荐答案

在一些更多的挖掘,也许你应该设置XmlResolver该XmlReaderSettings的属性对象为null。

After some more digging, maybe you should set the XmlResolver property of the XmlReaderSettings object to null.

的的XmlResolver用于定位和   打开XML实例文档,或   找到并打开任何外部资源   由XML实例中引用   文件。这可以包括实体   DTD或模式。

'The XmlResolver is used to locate and open an XML instance document, or to locate and open any external resources referenced by the XML instance document. This can include entities, DTD, or schemas.'

所以,code是这样的:

So the code would look like this:

        XmlReaderSettings settings = new XmlReaderSettings();
        settings.XmlResolver = null;
        settings.DtdProcessing = DtdProcessing.Parse;
        XmlDocument doc = new XmlDocument();
        using (StringReader sr = new StringReader(xml))
            using (XmlReader reader = XmlReader.Create(sr, settings))
            {
                doc.Load(reader);
            }