Results 1 to 6 of 6

Thread: XML Saving error

  1. #1

    XML Saving error

    I get the following error when i try to add a new <person> to my XML document.
    System.InvalidOperationException: This document already has a 'DocumentElement' node.
    The XML file looks like this:
    HTML Code:
    <?xml version="1.0" encoding="utf-8" ?>
    <usersettings>
      <person>
        <name>Alex</name>
        <age>14</age>
        <lang>PHP</lang>
      </person>
      <person>
        <name>Swagner</name>
        <age>14</age>
        <lang>HTML/CSS</lang>
      </person>
    </usersettings>
    I tried to add a new <person> element. I can correctly read the file at the moment, but I can't append a new <person> to the object (I can to a brand new XML document but that's not what I want).

    Here's my code:

    Code:
    class Program
        {
            private const string FILE = @"xml/usersettings.xml";
            static void Main(string[] args)
            {
                try
                {
                    XmlDocument xml = new XmlDocument();
                    xml.Load(FILE);
                    XmlNodeList people = xml.GetElementsByTagName("person");
                    foreach (XmlNode node in people)
                    {
                        XmlElement person = (XmlElement)node;
    
                        string name = person.GetElementsByTagName("name")[0].InnerText;
                        int age = Convert.ToInt32(person.GetElementsByTagName("age")[0].InnerText);
                        string lang = person.GetElementsByTagName("lang")[0].InnerText;
    
                        Console.WriteLine("Name: {0}", name.ToString());
                        Console.WriteLine("Age: {0}", age.ToString());
                        Console.WriteLine("language: {0}\n", lang.ToString());
                    }
                    XmlElement newp = xml.CreateElement("person");
                    xml.AppendChild(newp);
                    xml.Save(FILE);
                }
                catch (Exception e)
                {
                    Console.WriteLine("Error: \n\n {0}", e.ToString());
                    System.IO.StreamWriter err = new System.IO.StreamWriter(@"err.txt");
                    err.WriteLine("Error Message:\n\n{0}", e.ToString());
                    err.Close();
                }
                finally
                {
                    string s = Console.ReadLine();
    
                }
            }
        }
    Web Design and Web Development blog
    Designer Notice: Browser List for design compatibility

  2. #2
    Try creating your new element in the parent of the node referenced by xml. Also, try using the debugger in VS by setting breakpoints and going line-by-line. That makes solving these things much easier

  3. #3
    Okay I will try when I get time.
    Thanks!
    Web Design and Web Development blog
    Designer Notice: Browser List for design compatibility

  4. #4
    I would assume I'd do this:
    Code:
    XmlElement newp = xml.ParentNode.CreateElement("person");
    But that doesn't work. ParentNode does not contain the CreateElement method.
    Web Design and Web Development blog
    Designer Notice: Browser List for design compatibility

  5. #5
    No, by parent node, I mean add your person to the usersettings node. Your xml value only stores person nodes...not their parent, which would be usersettings.

    Great, now even Kirupa is { facebooking | twittering }

  6. #6
    I think I understand, will try it tomorrow.
    Thank you!
    Web Design and Web Development blog
    Designer Notice: Browser List for design compatibility

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012