简体   繁体   中英

xml Element Delete form xml file wp8

Hello i try to delete xml element but code is run successfully but when i am check xml file data is not deleted Here is my Xml Data How to delete in xml file pageno is different Please Help

   <data>
  <Bookmarkdata>
    <Bookname>thepdftest</Bookname>
    <Bookid>57d86c55-1d9a-49d0-8b60-acdc0c283d24</Bookid>
    <Pageno>1</Pageno>
  </Bookmarkdata>
  <Bookmarkdata>
    <Bookname>thepdftest</Bookname>
    <Bookid>57d86c55-1d9a-49d0-8b60-acdc0c283d24</Bookid>
    <Pageno>2</Pageno>
  </Bookmarkdata>
  <Bookmarkdata>
    <Bookname>thepdftest</Bookname>
    <Bookid>57d86c55-1d9a-49d0-8b60-acdc0c283d24</Bookid>
    <Pageno>3</Pageno>
  </Bookmarkdata>
</data>

Here is my code

1.this is not retutn any error but not at remove

 doc.Descendants("Bookmarkdata")
    .Where(x => (string)x.Attribute("Pageno") == pageno)
    .Remove();

and i am try to with two where condition using and that return a error

 doc.Descendants("Bookmarkdata")
    .Where((x => (string)x.Attribute("Pageno") == pageno) &&
           (x => (string)x.Attribute("Bookname") == bookname))
    .Remove();

This is my Full code

private void DeleteBookMark(string bookname, string pageno)
{

    using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
    {
        if (!file.FileExists("BookmarkFile.xml"))
        {
            StreamResourceInfo sr_en = Application.GetResourceStream(new Uri("Resources\\BookmarkFile.xml", UriKind.Relative));
            using (BinaryReader br_en = new BinaryReader(sr_en.Stream))
            {
                byte[] data1 = br_en.ReadBytes((int)sr_en.Stream.Length);
                //Write the file.
                using (BinaryWriter bw = new BinaryWriter(file.CreateFile("BookmarkFile.xml")))
                {
                    bw.Write(data1);
                    bw.Close();
                }
            }
        }

        // work with file at isolatedstorage
        using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream("BookmarkFile.xml", FileMode.Open, file))
        {
            XDocument doc = XDocument.Load(stream, LoadOptions.None);

            doc.Descendants("Bookmarkdata")
                .Where((x => (string)x.Attribute("Pageno") == pageno) && (x => (string)x.Attribute("Bookname") == bookname))
                .Remove();


            // prevent xml file from doubling nodes
            if (stream.CanSeek)
                stream.Seek(0, SeekOrigin.Begin);
            doc.Save(stream);
        }
    }

}

You have two lambdas which you are comparing with && . Lambda is an anonymous method - its not a result of method execution. So, you are trying to apply conditional AND on methods, which gives you error.

Where operator accepts one delegate which should return boolean value. Here is correct syntax:

 doc.Descendants("Bookmarkdata")
    .Where(x => (string)x.Element("Pageno") == pageno && 
                (string)x.Element("Bookname") == bookname)
    .Remove();

NOTE: Both <Pageno> and <Bookname> are elements, not attributes.

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