Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8365445
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T12:39:30+00:00 2026-06-09T12:39:30+00:00

Using Linq to XML, I am trying to add an XElement to an existing

  • 0

Using Linq to XML, I am trying to add an XElement to an existing XML file.
It has to be done in the Windows Phone .NET framework.
Currently my XML file looks like this:

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

Which gives me an xml file like this:

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

Which is still not correct, any ideas anyone ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T12:39:31+00:00Added an answer on June 9, 2026 at 12:39 pm

    Initial xml file

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

    Following code add one new child to existing 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);
        }
    

    Result xml file

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

    Update

    Bug on save, you should set stream length to 0

    Explanation

    After reader existing file, stream does not remove any data

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

    So when you call, data has been appended

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

    Add stream.SetLength(0); before x.Save(stream); and all data will be overwritten.

    Here is full version

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

    Please note: I’ve removed return statements as useless.

    P.S. Take a look on resharper, it can improve code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to learn how to search XML trees with LINQ using VB.net.
I am trying to read an XML file using LINQ. I have had no
I'm trying to parse out an complex XML file using LINQ. The files contains
Here's what I'm trying to do. I'm querying an XML file using LINQ to
I am trying to read a value from an XML file using LINQ. This
I'm trying to find the inner text value of an element using LINQ-to-XML (an
When reading a XML file with linq to XML using a XDocument and there
I am using Linq To Xml to create an Xml file from DataSet. This
I am trying read rss feed of stack overflow using Linq to xml. I
I'm trying to extract some data from XML using Linq to XML, and I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.