添加新的XElement到的XDocument [英] Add new XElement to Xdocument

查看:204
本文介绍了添加新的XElement到的XDocument的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下面的代码,它成功地写入到一个XML文件。然而,每一次改写,因为所取得的tagRegistry.Save()调用。我如何添加一个新的XElement到现有的文件吗?目前,该文件只是覆盖。

 公共无效saveTag()
{
如果(文件.Exists(/ tagRegistry.xml))
{
的XElement tagRegistry = XElement.Load(/ tagRegistry.xml);
的XElement newTag =新的XElement(变量,
新的XElement(标签,stringUid),
新的XElement(名,desiredName),
新的XElement(纬度,纬度),
新的XElement(经度,经度));
tagRegistry.Add(newTag);

使用(VAR存储= IsolatedStorageFile.GetUserStoreForApplication())
{
使用(流流= storage.CreateFile(/ tagRegistry.xml))
{
tagRegistry.Save(流);
}
}

}
,否则
{
的XDocument tagRegistry =新的XDocument(新的XElement(SmartSafe));
tagRegistry.Element(SmartSafe)。加入(新的XElement(变量,
新的XElement(标签,stringUid),
新的XElement(名,desiredName)
新的XElement(纬度,纬度),
新的XElement(经度,经度)));使用
使用(VAR存储= IsolatedStorageFile.GetUserStoreForApplication())
{
(流流= storage.CreateFile(/ tagRegistry.xml))
{
tagRegistry.Save(流);
}
}
}
}


解决方案

试试这个:

 公共无效saveTag()
{
使用(VAR存储= IsolatedStorageFile.GetUserStoreForApplication())
{
的XDocument文件;
的XElement tagRegistry = NULL;
$如果(storage.FileExists(/ tagRegistry.xml))
{
使用(VAR流= storage.OpenFile(/ tagRegistry.xml,FileMode.Open B $ b ))
{
=文件XDocument.Load(流);
}

tagRegistry = document.Descendants(SmartSafe)FirstOrDefault();
}
,否则
{
=文件新的XDocument();
}

如果(tagRegistry == NULL)
{
tagRegistry =新的XElement(SmartSafe);
document.Add(tagRegistry);
}

的XElement newTag =新的XElement(变量,
新的XElement(标签,stringUid),
新的XElement(名,desiredName) ,
新的XElement(纬度,纬度),
新的XElement(经度,经度));

tagRegistry.Add(newTag);使用(流流= storage.CreateFile(/ tagRegistry.xml))
{
document.Save(流)

;
}
}
}


I have the following code, which successfully writes to an XML file. However, it overwrites each time because of the tagRegistry.Save() call being made. How can I add a new XElement to the existing file? 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);
        }
    }
}

这篇关于添加新的XElement到的XDocument的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆