如何加载用于 XDocument 的内联 DTD? [英] How to load an inline DTD for use with XDocument?

查看:13
本文介绍了如何加载用于 XDocument 的内联 DTD?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,关于如何在 WP7 中将文档类型定义包含到 XML 文件中,或从 XML 文件加载到 XDocument 中.我有与此类似的 DTD 文件:

I have a question regarding how to include a document type definition into an XML file, or from and XML file, that is being loaded into XDocument, in WP7. I have DTD file that is similar to this:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE root [
 <!ELEMENT root (Person*)>

 <!ELEMENT Person (Name*, Description*)>
 <!ELEMENT Name (#PCDATA)>
 <!ELEMENT Description (#PCDATA)>

 <!ENTITY egrave "&#232;">
 <!ENTITY eacute "&#233;">
 <!ENTITY euro  "&#8364;">
]>

我需要将此 DTD 添加到 XML 我要捕获特殊字符,例如 &eacute;.我使用以下方法从网络获取 XML 以在 Linq 中使用:

I need to add this DTD to the XML I'm getting to catch special characters, such as &eacute;. I am getting the XML from the web to use in Linq using the following method:

private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
  string documentUrl = "http://www.example.com";

  WebClient client = new WebClient();

  client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted);
  client.OpenReadAsync(new Uri(documentUrl, UriKind.Absolute));
}

void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
  Stream str = e.Result;

  XDocument data = XDocument.Load(str);

  // Saving the XML to the file system for later use
  IsolatedStorageFile isoFile = IsolatedStorageFile.GetUserStoreForApplication();
  IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream("my.xml", FileMode.OpenOrCreate, isoFile);
  StreamWriter sw = new StreamWriter(isoStream);
  XmlWriter xw = XmlWriter.Create(isoStream);

  data.Save(xw);

  // Creating a list to populate a listbox
  List<MyObject> list1 = new List<MyObject>();

  items = (from query in data.Descendants("Person")
    select new MyObject()
    {
    // Doing stuff here...
    }).ToList();

  listBox1.ItemsSource = items;

}

如果 DTD 被内联,即在实际的 XML 本身中,XDocument 似乎不会传递 XML.我已经尝试了多种基于 this 使用 XDocumentType 的方法 帖子,但我无法弄清楚.我该怎么做?

It seems that XDocument won't pass the XML if the DTD is put inline, i.e. in the actual XML itself. I've tried many ways of using XDocumentType based on this post, but I can't figure it out. How can I do this?

推荐答案

阅读 XML 文档时需要启用 DTD 处理.为此,请使用 XmlReader 具有适当的设置:

You need to enable DTD processing when you read the XML document. To do that use a XmlReader with appropriate settings:

var settings = new XmlReaderSettings { DtdProcessing = DtdProcessing.Parse };
XmlReader reader = XmlReader.Create(str, settings);
XDocument data = XDocument.Load(reader);

如果你想要外部的 DTD,你需要指定一个 XmlResolver 在设置中:

If you want to have the DTD external you need to specify a XmlResolver in the settings:

var settings = new XmlReaderSettings
{
    DtdProcessing = DtdProcessing.Parse,
    XmlResolver = /* some resolver here */,
};

默认的 XmlResolver 是一个 XmlUrlResolver 无需使用凭据即可解析 URL.您可能需要考虑从本地源解析 DTD.为此,您可以使用预先填充的 XmlPreloadedResolver.

The default XmlResolver is an XmlUrlResolver which resolves URLs without using credentials. You may want to consider resolving the DTD from a local source instead. For that you can use a prepopulated XmlPreloadedResolver.

这篇关于如何加载用于 XDocument 的内联 DTD?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆