I am storing XML data in IsolatedStorage, while reading data from IsolatedStorage .I need to convert StreamReader to XDocument. Following code i have used to convert StreamReader to XDocument. I am getting a error like : "root element is missing"
using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
IsolatedStorageFileStream isoFileStream = myIsolatedStorage.OpenFile("AllHeadLine.xml", FileMode.Open);
using (StreamReader reader = new StreamReader(isoFileStream))
{
displayXmlData.Text = reader.ReadToEnd();
XDocument offlineHeadline = XDocument.Load(reader);
}
}
EDIT : XML content
<category><catname>ರಾಜ್ಯ</catname><img>http://www.udayavani.com/udayavani_cms/gall_content/2013/3/2013_3$thumbimg113_Mar_2013_235853890.jpg</img><heading>ನನ್ನ ಮಗನ ಬಗ್ಗೆ ಹೆಮ್ಮೆ ಇದೆ</heading><navigateurl>some Url </navigateurl></category>
How to solve this ?
Look at what you're doing:
using (StreamReader reader = new StreamReader(isoFileStream))
{
displayXmlData.Text = reader.ReadToEnd();
XDocument offlineHeadline = XDocument.Load(reader);
}
You're reading all the data out of the StreamReader via ReadToEnd, and then you're trying to load it into an XDocument. There's no more data to read! A few options:
displayXmlData.Text and parse to a document with XDocument.Parse. (If that's not supported on WP7, use a StringReader and XDocument.LoadReadToEnd call entirely, and live without setting displayXmlData.Text. It's not clear whether this was required or just for diagnostic purposes anyway.Unless you really need the text of the file verbatim, I'd actually avoid creating the StreamReader at all, and load directly from the Stream. That will let LINQ to XML do the encoding detection too.
using (var storage = IsolatedStorageFile.GetUserStoreForApplication())
{
using (var stream = storage.OpenFile("AllHeadLine.xml", FileMode.Open))
{
XDocument offlineHeadline = XDocument.Load(stream);
...
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With