Prevent DTD download when parsing XML

前端 未结 5 833
北恋
北恋 2020-12-09 01:44

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?

5条回答
  •  心在旅途
    2020-12-09 02:26

    Try something like this:

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

    The thing to note here is that XmlReaderSettings has the ProhibitDtd property set to true by default.

提交回复
热议问题