Windows 使用LINQtoXML将XElement添加到XML文件

Windows 使用LINQtoXML将XElement添加到XML文件,windows,silverlight,windows-phone-7,xelement,Windows,Silverlight,Windows Phone 7,Xelement,使用LINQtoXML,我试图将XElement添加到现有XML文件中。 它必须在Windows Phone.NET框架中完成。 当前我的XML文件如下所示: <?xml version="1.0" encoding="utf-8"?> <Kids> <Child> <Name>Kid1</Name> <FirstName>hisname</FirstName

使用LINQtoXML,我试图将XElement添加到现有XML文件中。 它必须在Windows Phone.NET框架中完成。 当前我的XML文件如下所示:

    <?xml version="1.0" encoding="utf-8"?>
    <Kids>
      <Child>
        <Name>Kid1</Name>
        <FirstName>hisname</FirstName>
      </Child>
    </Kids>

and my code looks like this:



    using (IsolatedStorageFileStream stream = 
               new IsolatedStorageFileStream("YourKids.xml", fileMode, store))
    { 
        XDocument x = XDocument.Load(stream);
        XElement t =  new XElement("Child",
                                           new XElement("Name", pName),
                                           new XElement("FirstName", pFirstname));

       t.Add(from el in x.Elements()
             where el == el.Descendants("Child").Last()
             select el);

       x.Save(stream);
    }

this doesn't do what I want to achieve. I want to add a new "Child" element to the the exisiting XML file like this :

     <?xml version="1.0" encoding="utf-8"?>
    <Kids>
      <Child>
        <Name>Kid1</Name>
        <FirstName>hisname</FirstName>
      </Child>
    <Child>
        <Name>kid2</Name>
        <FirstName>SomeName</FirstName>
      </Child>
    </Kids>

Could use some help because I am stuck ;-)

After the tips from GSerjo, my code looks like this now:

 try
            {

                if (store.FileExists("YourKids.xml"))
                {



                    using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream("YourKids.xml",FileMode.Open, store))
                    {


                        var x = XElement.Load(stream);
                        var t =  new XElement("Child",
                                                        new XElement("Name", pName),
                                                        new XElement("FirstName", pFirstname)
                                                                  );

                        x.Add(t);

                        x.Save(stream);




                        stream.Close();
                        return;
                    }

                }
                else
                {



                    using (IsolatedStorageFileStream CreateIsf = new IsolatedStorageFileStream("YourKids.xml",FileMode.Create,store))
                    {

                        var xDoc = new XElement("Kids",
                                                     new XElement("Child",
                                                       new XElement("Name", pName),
                                                       new XElement("FirstName", pFirstname)
                                                                 )

                                                  );

                        xDoc.Save(CreateIsf);
                        CreateIsf.Close();

                        return;
                    }
                }
            }
            catch (Exception ex)
            {
              message = ex.Message;
            }
<?xml version="1.0" encoding="utf-8"?>
<Kids>
  <Child>
    <Name>test</Name>
    <FirstName>test</FirstName>
  </Child>
</Kids><?xml version="1.0" encoding="utf-8"?>
<Kids>
  <Child>
    <Name>test</Name>
    <FirstName>test</FirstName>
  </Child>
  <Child>
    <Name>testing</Name>
    <FirstName>testing</FirstName>
  </Child>
</Kids>

基德1
他的名字
我的代码如下所示:
使用(IsolatedStorage文件流=
新的IsolatedStorageFileStream(“yourchids.xml”,fileMode,store))
{ 
XDocument x=XDocument.Load(流);
XElement t=新的XElement(“子元素”,
新XElement(“名称”,pName),
新XElement(“FirstName”,pFirstname));
t、 添加(来自x.Elements()中的el)
其中el==el.subjections(“子”).Last()
选择el);
x、 保存(流);
}
这不是我想要达到的。我想向现有XML文件添加一个新的“Child”元素,如下所示:
基德1
他的名字
基德2
名字
可能需要一些帮助,因为我卡住了;-)
根据GSerjo的提示,现在我的代码如下所示:
尝试
{
如果(store.FileExists(“yourchids.xml”))
{
使用(IsolatedStorageFileStream=new IsolatedStorageFileStream(“YourKids.xml”,FileMode.Open,store))
{
var x=XElement.Load(流);
var t=新的元素(“子元素”,
新XElement(“名称”,pName),
新XElement(“FirstName”,pFirstname)
);
x、 加(t);
x、 保存(流);
stream.Close();
返回;
}
}
其他的
{
使用(IsolatedStorageFileStream CreateIsf=new IsolatedStorageFileStream(“yourchids.xml”,FileMode.Create,store))
{
var xDoc=new-XElement(“孩子们”,
新元素(“子元素”,
新XElement(“名称”,pName),
新XElement(“FirstName”,pFirstname)
)
);
xDoc.Save(CreateIsf);
CreateIsf.Close();
返回;
}
}
}
捕获(例外情况除外)
{
消息=例如消息;
}
这给了我一个如下的xml文件:

    <?xml version="1.0" encoding="utf-8"?>
    <Kids>
      <Child>
        <Name>Kid1</Name>
        <FirstName>hisname</FirstName>
      </Child>
    </Kids>

and my code looks like this:



    using (IsolatedStorageFileStream stream = 
               new IsolatedStorageFileStream("YourKids.xml", fileMode, store))
    { 
        XDocument x = XDocument.Load(stream);
        XElement t =  new XElement("Child",
                                           new XElement("Name", pName),
                                           new XElement("FirstName", pFirstname));

       t.Add(from el in x.Elements()
             where el == el.Descendants("Child").Last()
             select el);

       x.Save(stream);
    }

this doesn't do what I want to achieve. I want to add a new "Child" element to the the exisiting XML file like this :

     <?xml version="1.0" encoding="utf-8"?>
    <Kids>
      <Child>
        <Name>Kid1</Name>
        <FirstName>hisname</FirstName>
      </Child>
    <Child>
        <Name>kid2</Name>
        <FirstName>SomeName</FirstName>
      </Child>
    </Kids>

Could use some help because I am stuck ;-)

After the tips from GSerjo, my code looks like this now:

 try
            {

                if (store.FileExists("YourKids.xml"))
                {



                    using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream("YourKids.xml",FileMode.Open, store))
                    {


                        var x = XElement.Load(stream);
                        var t =  new XElement("Child",
                                                        new XElement("Name", pName),
                                                        new XElement("FirstName", pFirstname)
                                                                  );

                        x.Add(t);

                        x.Save(stream);




                        stream.Close();
                        return;
                    }

                }
                else
                {



                    using (IsolatedStorageFileStream CreateIsf = new IsolatedStorageFileStream("YourKids.xml",FileMode.Create,store))
                    {

                        var xDoc = new XElement("Kids",
                                                     new XElement("Child",
                                                       new XElement("Name", pName),
                                                       new XElement("FirstName", pFirstname)
                                                                 )

                                                  );

                        xDoc.Save(CreateIsf);
                        CreateIsf.Close();

                        return;
                    }
                }
            }
            catch (Exception ex)
            {
              message = ex.Message;
            }
<?xml version="1.0" encoding="utf-8"?>
<Kids>
  <Child>
    <Name>test</Name>
    <FirstName>test</FirstName>
  </Child>
</Kids><?xml version="1.0" encoding="utf-8"?>
<Kids>
  <Child>
    <Name>test</Name>
    <FirstName>test</FirstName>
  </Child>
  <Child>
    <Name>testing</Name>
    <FirstName>testing</FirstName>
  </Child>
</Kids>

测试
测试
测试
测试
测试
测试
这仍然是不正确的,有人有什么想法吗?

初始xml文件

<?xml version="1.0" encoding="utf-8"?>
<Kids>
  <Child>
    <Name>Kid1</Name>
    <FirstName>hisname</FirstName>
  </Child>
</Kids>
结果xml文件

<?xml version="1.0" encoding="utf-8"?>
<Kids>
  <Child>
    <Name>Kid1</Name>
    <FirstName>hisname</FirstName>
  </Child>
  <Child>
    <Name>NewName</Name>
    <FirstName>NewFirstName</FirstName>
  </Child>
</Kids>
因此,当您调用时,数据已附加

   x.Save(stream);
   stream.Close();
添加stream.SetLength(0)x之前保存(流)和所有数据将被覆盖

这是完整的版本

            if (store.FileExists("YourKids.xml"))
            {
                using (var stream = new IsolatedStorageFileStream("YourKids.xml", FileMode.Open,
                                                                                     store))
                {
                    var x = XElement.Load(stream);
                    var t = new XElement("Child",
                                         new XElement("Name", pName),
                                         new XElement("FirstName", pFirstname)
                        );
                    x.Add(t);
                    stream.SetLength(0);
                    x.Save(stream);
                    stream.Close();
                }
            }
            else
            {
                using (var CreateIsf = new IsolatedStorageFileStream("YourKids.xml", FileMode.Create, store))
                {
                    var xDoc = new XElement("Kids",
                                            new XElement("Child",
                                                         new XElement("Name", pName),
                                                         new XElement("FirstName", pFirstname)
                                                )
                        );
                    xDoc.Save(CreateIsf);
                    CreateIsf.Close();
                }
            }
请注意:我已经删除了无用的返回语句


另外,看看resharper,它可以改进代码。

哇,谢谢你的快速回复。虽然它将新的子元素添加到我的XML文件中,但它保留了我的初始XML文件,并将结果XML文件添加到我的初始文件下,使XML文件看起来像:Kid1 hisname Kid1 hisname NewFirstName看起来像是不正确的保存结果,root.save(filPath)-替换现有文件。请显示您的上一个代码。我已经用完整的代码编辑了我的第一篇文章,这样您就可以看到我是如何保存xml文件的。谢谢GSerjo,非常感谢。