SL4: Root element is missing
I know this has been asked elsewhere, but none of the questions or answers helped.
I open an xml file in my SL 4 app:StreamResouceInfo sri = Application.GetResourceStream(new System.Uri("z.xml", UriKind.Relative));
if (null != sri)
{
XDocument xDoc = XDocument.Load(sri.Stream);
}
"Root element is missing" exception.
The xml:
Hmm, can't seem to post the xml... It is well-formed and valid, with a single root node and all tags closed. Thanks开发者_JAVA百科 for any hints...Let's try it w/o the angle brackets:
xml version="1.0" encoding="utf-8"
Root
Collection name="Fonts"
Value Lucida /Value
Value Arial /Value
/Collection
/Root
I just had this very issue. I ended up just first putting the received stream into a XmlReader and then that into the XDocument.Load.
Your code would be
StreamResouceInfo sri = Application.GetResourceStream(new System.Uri("z.xml", UriKind.Relative));
if (null != sri)
{
XmlReader rdr = new XmlReader.Create(sri.Stream);
XDocument xDoc = XDocument.Load(rdr);
}
In my case i was using the WebClient.DownloadStringAsync call so it was a little different
void getCacheData_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error == null)
{
StringReader stream = new StringReader(e.Result);
XmlReader reader = XmlReader.Create(stream);
XDocument doc = new XDocument.Load(reader);
}
}
精彩评论