向Xdocument添加新的XElement

6

我有以下代码,它成功地将内容写入XML文件。然而,由于调用了tagRegistry.Save()方法,每次都会覆盖掉原来的文件。我该如何向现有文件中添加新的XElement元素呢?目前这个文件只是被简单地覆盖了。

public void saveTag()
{
    if (File.Exists("/tagRegistry.xml"))
    {
        XElement tagRegistry = XElement.Load("/tagRegistry.xml");
        XElement newTag = new XElement("Tag",
        new XElement("tag", stringUid),
        new XElement("name", desiredName),
        new XElement("latitude", latitude),
        new XElement("longitude", longitude));
        tagRegistry.Add(newTag);

        using (var storage = IsolatedStorageFile.GetUserStoreForApplication())
        {
            using (Stream stream = storage.CreateFile("/tagRegistry.xml"))
            {
                tagRegistry.Save(stream);
            }
        }

    }
    else
    {
        XDocument tagRegistry = new XDocument(new XElement("SmartSafe"));
        tagRegistry.Element("SmartSafe").Add(new XElement("Tag",
                    new XElement("tag", stringUid),
                    new XElement("name", desiredName),
                    new XElement("latitude", latitude),
                    new XElement("longitude", longitude)));
        using (var storage = IsolatedStorageFile.GetUserStoreForApplication())
        {
            using (Stream stream = storage.CreateFile("/tagRegistry.xml"))
            {
                tagRegistry.Save(stream);
            }
        }
    }
}

你不能这样做 - 唯一的方法就是像你已经做的那样,覆盖现有文件。 - McGarnagle
好的,那么我该如何创建一个新的XML文件,其中包含旧数据和新数据呢? - anthonyhumphreys
我已经在if语句的顶部完成了这个操作。如何将读取的数据添加到新文件中呢?非常感谢您迄今为止的帮助。 - anthonyhumphreys
啊,抱歉我没看到。我不知道为什么那样不起作用?你已经将“existing”加载到了“tagRegistry”,然后添加了“newTag”。 - McGarnagle
1
只是一个建议,不要以那种方式嵌套 using (...) 语句,首选标准是将它们放在彼此下面,而不使用括号或缩进。 - myermian
显示剩余2条评论
2个回答

13

试试这个:

public void saveTag()
{
    using (var storage = IsolatedStorageFile.GetUserStoreForApplication())
    {
        XDocument document;
        XElement tagRegistry = null;

        if (storage.FileExists("/tagRegistry.xml"))
        {
            using(var stream = storage.OpenFile("/tagRegistry.xml", FileMode.Open))
            {
                document = XDocument.Load(stream);
            }

            tagRegistry = document.Descendants("SmartSafe").FirstOrDefault();
        }
        else
        {
            document = new XDocument();
        }

        if(tagRegistry == null)
        {
            tagRegistry = new XElement("SmartSafe");
            document.Add(tagRegistry);
        }

        XElement newTag = new XElement("Tag",
            new XElement("tag", stringUid),
            new XElement("name", desiredName),
            new XElement("latitude", latitude),
            new XElement("longitude", longitude));

        tagRegistry.Add(newTag);

        using (Stream stream = storage.CreateFile("/tagRegistry.xml"))
        {
            document.Save(stream);
        }
    }
}

谢谢,我尝试过了,但似乎仍然会用新元素覆盖现有文件。不幸的是,在Windows Phone上无法使用existingDoc.Save(string)方法(据我所知,我可能错了)。 - anthonyhumphreys
1
它使用隔离存储中的流进行 IO 操作,并使用现有文件中的 SmartSafe 添加新标签。 - Patrick Hallisey
太棒了,非常感谢你。也感谢其他所有人的帮助,真的很感激。 - anthonyhumphreys

2

可能是您的File.Exists调用存在问题。 您正在将文件存储到隔离存储中,但从当前运行目录中读取。 所以每次都会落入else块,并编写新文件。


这很有道理,我简直不敢相信我错过了那个。下一个问题是如何查询Isostore中的文件? - anthonyhumphreys

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接