我有一个关于如何在 WP7 中将文档类型定义包含到 XML 文件或从 XML 文件中加载到 XDocument 中的问题。我有与此类似的 DTD 文件:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE root [
<!ELEMENT root (Person*)>
<!ELEMENT Person (Name*, Description*)>
<!ELEMENT Name (#PCDATA)>
<!ELEMENT Description (#PCDATA)>
<!ENTITY egrave "è">
<!ENTITY eacute "é">
<!ENTITY euro "€">
]>
我需要将此 DTD 添加到要捕获特殊字符的 XML 中,例如é
. 我使用以下方法从 Web 获取 XML 以在 Linq 中使用:
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。根据这篇文章,我尝试了多种使用 XDocumentType 的方法,但我无法弄清楚。我怎样才能做到这一点?