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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T09:43:52+00:00 2026-05-30T09:43:52+00:00

I am created a new xml document if one isn’t found, then opening it

  • 0

I am created a new xml document if one isn’t found, then opening it to create a new entry. However it just seems to be appending a completed new XML file to end of the blank file I created

after creation:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Devices />

after adding new xelement:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Devices /><?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Devices>
  <device>
    <name>blah</name>
    <src00>True</src00>
  </device>
</Devices>

my code … I’ve tried adding two different ways, both with the same results

           // Create file if not found
            if (!storage.FileExists("settings\\mydevices.xml"))
            {
                using (IsolatedStorageFileStream stream = storage.OpenFile("settings\\mydevices.xml", FileMode.Create, FileAccess.ReadWrite))
                {
                    XDocument devicesDoc =
                        new XDocument(
                            new XDeclaration("1.0", "utf-8", "yes"),
                            new XElement("Devices")
                            );

                    System.IO.StreamWriter file = new System.IO.StreamWriter(stream);
                    devicesDoc.Save(file);
                    file.Dispose();
                }
            }


            // Add new device
            using (IsolatedStorageFileStream stream = storage.OpenFile("settings\\mydevices.xml", FileMode.Open, FileAccess.ReadWrite))
            {

                XDocument mydevicesXml = XDocument.Load(stream);
                //XElement item= new XElement("device",
                //                new XElement("name",selectedDeviceName),
                //                new XElement("src00","True")
                //               );

                //mydevicesXml.Descendants("Devices").First().Add(item);
                //mydevicesXml.Element("Devices").Add(item);

                mydevicesXml.Root.Add(
                    new XElement("device",
                        new XElement("name", selectedDeviceName),
                        new XElement("src00", "True")));


                System.IO.StreamWriter file = new System.IO.StreamWriter(stream);
                mydevicesXml.Save(file);
                file.Dispose();
  • 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-30T09:43:54+00:00Added an answer on May 30, 2026 at 9:43 am

    Try:

    XElement root;
    
    if(File.Exists(file))
        root = XElement.Load(file);
    else
        root = new XElement("Devices");
    
    root.Add(new XElement("device",
             new XElement("name", selectedDeviceName),
             new XElement("src00", "True")));
    root.Save(file);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to manipulate an existing XML document, and create a new one from
So I set up a new project into Eclipse and created a build.xml that
I'm new in xml, sorry for the dumb question. I'm trying to create xsl
This is how I create XStream instance for XML: XStream xstream = new XStream();
using (var file_stream = File.Create(users.xml)) { var serializer = new XmlSerializer(typeof(PasswordManager)); serializer.Serialize(file_stream, this); file_stream.Close();
I have created an XML file and DTD which can be found at the
I get an XML from website into a string strXML. Then I create an
I have created a file with XmlDocument xmldoc = new XmlDocument(); Can I make
I've created new db via phpMyAdmin and while editing db privilages clicked delete localhost
I created a new SharePoint portal for testing purposes using a manually added HOSTS

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.