我有一个问题,关于如何将文档类型定义包含到XML文件中或从XML文件中加载到WP7的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中,即实际的XML本身中,XDocument不会传递XML。 我已经尝试了很多使用基于此帖子的XDocumentType的方法,但我无法弄清楚。 我该如何做到这一点?