添加到的XElement XML文件使用LINQ to XML(Adding XElement to

2019-09-21 03:56发布

使用LINQ to XML,我想一个的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文件是这样的:

<?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>

这仍然是不正确的,任何想法吗?

Answer 1:

最初的XML文件

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

下面的代码添加一个新的子到现有的XML

    [Test]
    public void Test()
    {
        string filPath = @"YourKids.xml";
        var root = XElement.Load(filPath);
         var newChild =  new XElement("Child",
                                   new XElement("Name", "NewName"),
                                   new XElement("FirstName", "NewFirstName"));
         root.Add(newChild);
        root.Save(filPath);
    }

结果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>

更新

在错误保存,你应该设置流长度为0

说明

读者现有文件之后, 不删除任何数据

using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream("YourKids.xml",FileMode.Open, store))
                    {
                        var x = XElement.Load(stream);

所以,当你打电话时,数据已被追加

   x.Save(stream);
   stream.Close();

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

下面是完整版

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

请注意:我已经删除return语句一样没用。

PS就以ReSharper的一看,它可以提高代码。



文章来源: Adding XElement to XML file using Linq to XML