简体   繁体   English

向 Xdocument 添加新的 XElement

[英]Add new XElement to Xdocument

I have the following code, which successfully writes to an XML file.我有以下代码,它成功写入 XML 文件。 However, it overwrites each time because of the tagRegistry.Save() call being made.但是,由于进行了 tagRegistry.Save() 调用,它每次都会覆盖。 How can I add a new XElement to the existing file?如何向现有文件添加新的 XElement? At the moment the file is simply overwritten.目前该文件只是被覆盖。

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);
            }
        }
    }
}

Try this:试试这个:

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);
        }
    }
}

It's possible your File.Exists call is wrong.您的File.Exists调用可能是错误的。 You are storing the file to isolated storage, but reading in from your current running directory.您将文件存储到独立存储,但从当前运行的目录中读取。 So you're always falling into the else block and writing a new file every time.所以你总是陷入else块并每次都写入一个新文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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