在 C# 中根据 DTD 验证 XML 时出现问题

发布于 2024-08-15 18:35:26 字数 946 浏览 5 评论 0原文

这已经困扰我几天了。我试图将上传的文件中的 XML 加载到 XmlDocument 对象中,并得到以下黄屏死机:

For security reasons DTD is prohibited in this XML document. To enable DTD processing set the ProhibitDtd property on XmlReaderSettings to false and pass the settings into XmlReader.Create method.

这是我的代码。您可以清楚地看到我将 ProhibitDtd 设置为 false。

public static XmlDocument LoadXml(FileUpload fu)
{
    var settings = new XmlReaderSettings
                       {
                           ProhibitDtd = false, 
                           ValidationType = ValidationType.DTD
                       };
    var sDtdPath = string.Format(@"{0}", HttpContext.Current.Server.MapPath("/includes/dtds/2.3/archivearticle.dtd"));
    settings.Schemas.Add(null, sDtdPath);

    var r = XmlReader.Create(new StreamReader(fu.PostedFile.InputStream), settings);
    var document = new XmlDocument();
    document.Load(r);
    return document;
}

This has been bugging me for a couple days. I'm trying to load a XML from an uploaded file to into an XmlDocument object and get the following yellow-screen-of-death:

For security reasons DTD is prohibited in this XML document. To enable DTD processing set the ProhibitDtd property on XmlReaderSettings to false and pass the settings into XmlReader.Create method.

Here's my code. You can clearly see I'm setting ProhibitDtd to false.

public static XmlDocument LoadXml(FileUpload fu)
{
    var settings = new XmlReaderSettings
                       {
                           ProhibitDtd = false, 
                           ValidationType = ValidationType.DTD
                       };
    var sDtdPath = string.Format(@"{0}", HttpContext.Current.Server.MapPath("/includes/dtds/2.3/archivearticle.dtd"));
    settings.Schemas.Add(null, sDtdPath);

    var r = XmlReader.Create(new StreamReader(fu.PostedFile.InputStream), settings);
    var document = new XmlDocument();
    document.Load(r);
    return document;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尘世孤行 2024-08-22 18:35:26

XmlResolver=null 添加到您的 XmlReaderSettings。这将阻止 xmlDocument 尝试访问 DTD。如果您需要验证,请在单独的操作中执行此操作。

Add XmlResolver=null to your XmlReaderSettings. This will prevent the xmlDocument from trying to access the DTD. If you need to validate, do that in a separate operation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文