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 6760451
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:03:56+00:00 2026-05-26T14:03:56+00:00

I have a little problem. I try to add an element in XML with

  • 0

I have a little problem. I try to add an element in XML with this code:

using (IsolatedStorageFile myStore = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (!myStore.FileExists("categorie_settings.xml"))
                {
                    using (IsolatedStorageFileStream myStream = new IsolatedStorageFileStream("categorie_settings.xml", FileMode.Create, myStore))
                    {
                        XNamespace _name = "";
                        XDocument new_doc = new XDocument(
                                new XDeclaration("1.0", "utf-8", ""),
                                new XElement(_name + "Root",
                                    new XElement("Row",
                                        new XElement("Nome", query.FirstOrDefault().Nome.ToString()),
                                        new XElement("Tipo", query.FirstOrDefault().Tipo.ToString()),
                                        new XElement("URL", query.FirstOrDefault().URL.ToString()),
                                        new XElement("Err", "Errore1")
                                        )));


                        new_doc.Save(myStream);

                    }
                }
                else
                {

                    using (IsolatedStorageFileStream myStream = new IsolatedStorageFileStream("categorie_settings.xml", FileMode.Open, myStore))
                    {
                        XDocument doc1 = XDocument.Load(myStream);
                        doc1.Element("Root").Add(
                            new XElement("Row",
                                new XElement("Nome", query.FirstOrDefault().Nome.ToString()),
                                new XElement("Tipo", query.FirstOrDefault().Tipo.ToString()),
                                new XElement("URL", query.FirstOrDefault().URL.ToString()),
                                new XElement("Err", "Errore2")));


                        doc1.Save(myStream);




                    }



                }

In the second Using I want to add an item to the XML file but the result after 2 call is this:

    <?xml version="1.0" encoding="utf-8"?>
<Root>
  <Row>
    <Nome>Homepage</Nome>
    <Tipo>News</Tipo>
    <URL>/web/ansait_web_rss_homepage.xml</URL>
    <Err>Errore1</Err>
  </Row>
</Root><?xml version="1.0" encoding="utf-8"?>
<Root>
  <Row>
    <Nome>Homepage</Nome>
    <Tipo>News</Tipo>
    <URL>/web/ansait_web_rss_homepage.xml</URL>
    <Err>Errore1</Err>
  </Row>
  <Row>
    <Nome>Cronache</Nome>
    <Tipo>News</Tipo>
    <URL>/web/notizie/rubriche/cronaca/cronaca_rss.xml</URL>
    <Err>Errore2</Err>
  </Row>
</Root>

It seems that adds to the file before the entire file plus the new element. How can I make just add the element?

  • 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-05-26T14:03:57+00:00Added an answer on May 26, 2026 at 2:03 pm

    Apologies for the false start (deleted). Here’s a snippet that would go into the second using-statement in your example. Note how we select the first Row element (firstRow) and then AddBeforeSelf. This assumes you want the new element at the top of the list.

    // See update, below, for corrected version
    
    XDocument doc1 = XDocument.Load( myStream );
    var root = doc1.Element( "Root" );
    var rows = root.Descendants( "Row" );
    var firstRow = rows.First();
    firstRow.AddBeforeSelf(
          new XElement( "Row",
              new XElement( "Nome", "Homepage2" ),
              new XElement( "Tipo", "News2" ),
              new XElement( "URL", "/web/xml2" ),
              new XElement( "Err", "Errore2" ) ) );
    doc1.Save( myStream );
    

    Updated: as the OP points out in the comments, he wanted the new element appended, not prepended. Additionally he gets an exception on subsequent invocations. That exception is due to XDocument.Save( Stream stream ) appending to the stream, so you wind up with two XML documents in the file (not well-formed XML). This following addresses both of those issues.

    XDocument doc1;
    using (IsolatedStorageFileStream myStream = new IsolatedStorageFileStream( "categorie_settings.xml", FileMode.Open, myStore ))
    {
        doc1 = XDocument.Load( myStream );
    }
    
    var root = doc1.Element( "Root" );
    var rows = root.Descendants( "Row" );
    var lastRow = rows.Last();
    lastRow.AddAfterSelf(
          new XElement( "Row",
              new XElement( "Nome", "Homepage2" ),
              new XElement( "Tipo", "News2" ),
              new XElement( "URL", "/web/xml2" ),
              new XElement( "Err", "Errore2" ) ) );
    
    using (IsolatedStorageFileStream myStream = new IsolatedStorageFileStream( "categorie_settings.xml", FileMode.Create, myStore ))
    {  
      doc1.Save( myStream );
    }
    

    There the save is done on a stream that creates the file, thereby overwriting the previous file. Alternatively you could use XDocument.Save( String filename ) which does replace the contents of the file.

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

Sidebar

Related Questions

I have this little problem, that I cannot figure out which arguments to pass
I have a little problem with a query. I'm selecting data using the between
I have a little problem about using jQuery (I really do not know jQuery
I have a little problem, I have an array and I want to add
We have this really weird problem in a Web Application when using ActiveRecord 2.0
i have a little problem when i try to attach a class to an
I have a little Problem in Blitzmax. I try to read an INI-file and
i have little problem with boost::asio library. My app receive and process data asynchronously,
I have a little problem with a Listview. I can load it with listview
I have a little problem with a simple vbScript. The script has to run

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.