简体   繁体   中英

How to get xml file from isolated in wp8

I Try to get Xml file and Bind to list Here is my Xml File

- <data>
  - <Bookmarkdata>
      <Bookname>TheNfame</Bookname> 
      <Bookid>5a1df538-6e91-4a39-819d-e043c9881fb7</Bookid> 
      <Pageno>0</Pageno> 
    </Bookmarkdata>
  - <Bookmarkdata>
      <Bookname>TheNfame</Bookname> 
      <Bookid>5a1df538-6e91-4a39-819d-e043c9881fb7</Bookid> 
      <Pageno>1</Pageno> 
    </Bookmarkdata>
 </data>

This is my code

private void GetBookMarkData() {

    var doc = new XDocument();
    XDocument xdoc=  new XDocument();
    using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
    {
        try
        {
            if (store.FileExists("BookmarkFile.xml"))
            {
                using (var sr = new StreamReader(new IsolatedStorageFileStream("BookmarkFile.xml", FileMode.OpenOrCreate, store)))
                {
                    doc = XDocument.Load(sr);
                   // xdoc = XDocument.Load(sr);
                    var data = from query in doc.Descendants("data")
                               select new Bookmarkdata
                               {
                                   Bookname = (string)query.Element("Bookname"),
                                   Bookid = (string)query.Element("Bookid"),
                                   BookPath = (string)query.Element("BookPath"),
                                   Pageno = (int)query.Element("Pageno")
                               };
                    Bookmark_List.ItemsSource = data;

                }
            }
        }
        catch
            (Exception ex) { }
    }

But in Catch return a error Value cannot be null.

Parameter name: element

In my code doc = XDocument.Load(sr); i have get a xml data dubag time but in next line is error Please Help

Your LINQ-to-XML query is a bit off. query variable represent <data> element here :

var data = from query in doc.Descendants("data")
                               select new Bookmarkdata
                               {
                                   Bookname = (string)query.Element("Bookname"),
                                   Bookid = (string)query.Element("Bookid"),
                                   BookPath = (string)query.Element("BookPath"),
                                   Pageno = (int)query.Element("Pageno")
                               };

so it doesn't have direct child named Bookname, Bookid, BookPath, or Pageno. I guess you want to select from <Bookmarkdata> elements instead :

var data = from query in doc.Descendants("Bookmarkdata")
                       select new
                       {
                           Bookname = (string)query.Element("Bookname"),
                           Bookid = (string)query.Element("Bookid"),
                           BookPath = (string)query.Element("BookPath"),
                           Pageno = (int)query.Element("Pageno")
                       };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM