Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将新XElement添加到Xdocument_C#_Xml_Windows Phone 8_Nfc - Fatal编程技术网

C# 将新XElement添加到Xdocument

C# 将新XElement添加到Xdocument,c#,xml,windows-phone-8,nfc,C#,Xml,Windows Phone 8,Nfc,我有以下代码,它成功地写入XML文件。但是,由于正在进行tagRegistry.Save()调用,它每次都会覆盖。如何向现有文件添加新的XElement?目前,该文件只是被覆盖 public void saveTag() { if (File.Exists("/tagRegistry.xml")) { XElement tagRegistry = XElement.Load("/tagRegistry.xml"); XElement newTag

我有以下代码,它成功地写入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);
            }
        }
    }
}
试试这个:

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

可能是您的
文件.Exists
调用错误。您正在将文件存储到独立存储中,但正在从当前运行的目录读入。所以你总是陷入
else
块中,每次都要写一个新文件。

你不能——唯一的方法是,按照你所做的,覆盖现有文件。好的,那么我如何创建一个新的XML文件,包含旧数据,同时也包含新数据?我在if语句的块顶部做了这件事。如何将读取的数据添加到新文件。。。?非常感谢你的帮助,很抱歉我没看到。我不明白为什么这样不行?您已将现有文件加载到
tagRegistry
,然后添加了
newTag
。建议您不要使用(…)语句嵌套
,最好的标准是将它们放在彼此下方,不带括号或缩进。谢谢,尝试过,但似乎仍仅用新元素覆盖现有文件。不幸的是,现有的doc.Save(string)方法在Windows Phone上不可用(据我所知,我可能错了),它使用来自独立存储的流进行io,并使用现有文件中的SmartSafe添加新标记。非常感谢。也谢谢大家,非常感谢你们的帮助。这很有道理,真不敢相信我错过了。下一个问题是如何查询该文件的Isostore?