无法在Windows Phone 8中更新XML文件的值

时间:2014-09-29 05:48:23

标签: c# xml windows-phone-8

每当我按下应用栏中的unavorite图标时,我想将属性pro_idprod_xml设置为空。毕竟,这些值设置为空,但是它还向XML文件添加了一个根元素,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<favorites>
  <favorite id="1" pro_id="" prod_xml="" />
  <favorite id="2" pro_id="" prod_xml="" />
  <favorite id="3" pro_id="" prod_xml="" />
  <favorite id="4" pro_id="" prod_xml="" />
</favorites> />
</favorites>

这是我从空白创建xml文件的方法(无错误):

public void createFavXML()
{
    // Get the local folder.
    System.IO.IsolatedStorage.IsolatedStorageFile local =
        System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForApplication();

    // Create a new folder named DataFolder.
    if (!local.DirectoryExists("Favorite"))
            local.CreateDirectory("Favorite");
 //       local.DeleteFile("Favorite\\favorite.xml");
        if (!local.FileExists("Favorite\\favorite.xml"))
        {
            // Create a new file named DataFile.txt.
            using (var isoFileStream = 
new System.IO.IsolatedStorage.IsolatedStorageFileStream("Favorite\\Favorite.xml", System.IO.FileMode.OpenOrCreate, local))
        {

            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            using (XmlWriter writer = XmlWriter.Create(isoFileStream, settings))
            {
                writer.WriteStartElement("favorites", "");
                // favorite id 1
                writer.WriteStartElement("favorite", "");
                // attribute id
                writer.WriteStartAttribute("id", "");
                writer.WriteString("1");
                writer.WriteEndAttribute();
                // attribute product id
                writer.WriteStartAttribute("pro_id", "");
                writer.WriteString("");
                writer.WriteEndAttribute();
                // attribute prod_xml
                writer.WriteStartAttribute("prod_xml", "");
                writer.WriteString("");
                writer.WriteEndAttribute();
                writer.WriteEndElement();

                // favorite id 2
                writer.WriteStartElement("favorite", "");
                // attribute id
                writer.WriteStartAttribute("id", "");
                writer.WriteString("2");
                writer.WriteEndAttribute();
                // attribute product id
                writer.WriteStartAttribute("pro_id", "");
                writer.WriteString("");
                writer.WriteEndAttribute();
                // attribute prod_xml
                writer.WriteStartAttribute("prod_xml", "");
                writer.WriteString("");
                writer.WriteEndAttribute();
                writer.WriteEndElement();

                // favorite id 3
                writer.WriteStartElement("favorite", "");
                // attribute id
                writer.WriteStartAttribute("id", "");
                writer.WriteString("3");
                writer.WriteEndAttribute();
                // attribute product id
                writer.WriteStartAttribute("pro_id", "");
                writer.WriteString("");
                writer.WriteEndAttribute();
                // attribute prod_xml
                writer.WriteStartAttribute("prod_xml", "");
                writer.WriteString("");
                writer.WriteEndAttribute();
                writer.WriteEndElement();

                // favorite id 4
                writer.WriteStartElement("favorite", "");
                // attribute id
                writer.WriteStartAttribute("id", "");
                writer.WriteString("4");
                writer.WriteEndAttribute();
                // attribute product id
                writer.WriteStartAttribute("pro_id", "");
                writer.WriteString("");
                writer.WriteEndAttribute();
                // attribute prod_xml
                writer.WriteStartAttribute("prod_xml", "");
                writer.WriteString("");
                writer.WriteEndAttribute();
                writer.WriteEndElement();



                // Ends the document
                writer.WriteEndElement();

                writer.Flush();
            }


        }
    }

}

这是我将数据写入xml文件以获取传递给它的特定id(工作正常)

   public void writeTOFavxml(string favId,string pro_id,string prod_xml)
    {

        var storage = IsolatedStorageFile.GetUserStoreForApplication();
        fileName = "Favorite\\Favorite.xml";
        using (IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream(fileName, FileMode.Open, storage))
        {

                   XDocument doc = XDocument.Load(isoStream);
                    foreach (var item in (from item in doc.Descendants("favorite")
                                     where item.Attribute("id").Value.Equals(favId)
                                     select item).ToList())
                    {

                        item.Attribute("pro_id").SetValue(pro_id);
                        item.Attribute("prod_xml").SetValue(prod_xml);

                    }                  
                    //First way
                    isoStream.Position = 0;
                    doc.Save(isoStream);                  
                }

    }

在prod_id和prod_xml属性有数据之后,我希望能够将它再次设置为空,以便传递给UnwriteXML()方法的特定id(问题在这里)

public void UnwriteXML(string pro_id)
{
    var storage = IsolatedStorageFile.GetUserStoreForApplication();
    fileName = "Favorite\\Favorite.xml";
    XDocument docx = null;
    using (IsolatedStorageFileStream isoStreamx = new IsolatedStorageFileStream(fileName, FileMode.Open, storage))
    {
        docx = XDocument.Load(isoStreamx);
        foreach (var item in (from item in docx.Descendants("favorite")
                                      where item.Attribute("pro_id").Value.Equals(pro_id)
                                      select item).ToList())
        {
            item.Attribute("pro_id").SetValue("");
            item.Attribute("prod_xml").SetValue("");
        }

        //First way
        isoStreamx.Position = 0;
        docx.Save(isoStreamx);
    }  
}

我通过将isoStream设置为位置0,按照与updating an existing xml file in Windows Phone中的问题完全相同的步骤,因为我认为他和我有同样的问题,但问题仍然存在。

1 个答案:

答案 0 :(得分:0)

你应该设置你的流长度,这将解决问题

stream.SetLength(xdoc.ToString().Length);