Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. This operation would create an incorrectly structured document.

This operation would create an incorrectly structured document.

Scheduled Pinned Locked Moved C#
helpcomxmlannouncement
9 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jassim Rahma
    wrote on last edited by
    #1

    Hi, I am trying the following code to create a new XML document but getting the above error. could you please help..

    xml_document = new XDocument();

    xml_document.Declaration = new XDeclaration("1.0", "utf-8", "true");

    xml_document.Add(new XElement("EventLog"));
    XElement event_node = new XElement("Event", new XAttribute("ID", Guid.NewGuid().ToString()));
    XElement event_date = new XElement("DateTime", DateTime.UtcNow.ToString("ddd dd MMM yyyy HH:mm:ss"));
    XElement event_details = new XElement("Description", event_description);

    event_node.Add(event_date);
    event_node.Add(event_details);

    //append element
    xml_document.Add(event_node);

    xml_document.Save("eventlog.xml");

    Technology News @ www.JassimRahma.com

    R 2 Replies Last reply
    0
    • J Jassim Rahma

      Hi, I am trying the following code to create a new XML document but getting the above error. could you please help..

      xml_document = new XDocument();

      xml_document.Declaration = new XDeclaration("1.0", "utf-8", "true");

      xml_document.Add(new XElement("EventLog"));
      XElement event_node = new XElement("Event", new XAttribute("ID", Guid.NewGuid().ToString()));
      XElement event_date = new XElement("DateTime", DateTime.UtcNow.ToString("ddd dd MMM yyyy HH:mm:ss"));
      XElement event_details = new XElement("Description", event_description);

      event_node.Add(event_date);
      event_node.Add(event_details);

      //append element
      xml_document.Add(event_node);

      xml_document.Save("eventlog.xml");

      Technology News @ www.JassimRahma.com

      R Offline
      R Offline
      Ravi Bhavnani
      wrote on last edited by
      #2

      Shouldn't event_node be added to the root "EventLog" node? /ravi

      My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com

      M J 2 Replies Last reply
      0
      • R Ravi Bhavnani

        Shouldn't event_node be added to the root "EventLog" node? /ravi

        My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com

        M Offline
        M Offline
        Manfred Rudolf Bihy
        wrote on last edited by
        #3

        Exactly! OP is trying to add more than one root node to a document which is of course nonsense. Regards,

        — Manfred

        "I had the right to remain silent, but I didn't have the ability!"

        Ron White, Comedian

        1 Reply Last reply
        0
        • R Ravi Bhavnani

          Shouldn't event_node be added to the root "EventLog" node? /ravi

          My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com

          J Offline
          J Offline
          Jassim Rahma
          wrote on last edited by
          #4

          How can I fix it? My xml structure is like this:

          Technology News @ www.JassimRahma.com

          J Richard DeemingR R 3 Replies Last reply
          0
          • J Jassim Rahma

            How can I fix it? My xml structure is like this:

            Technology News @ www.JassimRahma.com

            J Offline
            J Offline
            JV9999
            wrote on last edited by
            #5

            Empty structures are the best! ;P I guess something went wrong there?

            1 Reply Last reply
            0
            • J Jassim Rahma

              How can I fix it? My xml structure is like this:

              Technology News @ www.JassimRahma.com

              Richard DeemingR Online
              Richard DeemingR Online
              Richard Deeming
              wrote on last edited by
              #6

              As Ravi said, you need to add the event_node to the root node, not the document:

              xml_document.Root.Add(event_node);


              "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

              "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

              1 Reply Last reply
              0
              • J Jassim Rahma

                How can I fix it? My xml structure is like this:

                Technology News @ www.JassimRahma.com

                R Offline
                R Offline
                Ravi Bhavnani
                wrote on last edited by
                #7

                See my reply here[^]. /ravi

                My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com

                1 Reply Last reply
                0
                • J Jassim Rahma

                  Hi, I am trying the following code to create a new XML document but getting the above error. could you please help..

                  xml_document = new XDocument();

                  xml_document.Declaration = new XDeclaration("1.0", "utf-8", "true");

                  xml_document.Add(new XElement("EventLog"));
                  XElement event_node = new XElement("Event", new XAttribute("ID", Guid.NewGuid().ToString()));
                  XElement event_date = new XElement("DateTime", DateTime.UtcNow.ToString("ddd dd MMM yyyy HH:mm:ss"));
                  XElement event_details = new XElement("Description", event_description);

                  event_node.Add(event_date);
                  event_node.Add(event_details);

                  //append element
                  xml_document.Add(event_node);

                  xml_document.Save("eventlog.xml");

                  Technology News @ www.JassimRahma.com

                  R Offline
                  R Offline
                  Ravi Bhavnani
                  wrote on last edited by
                  #8

                  Try this:

                  xml_document = new XDocument();
                  xml_document.Declaration = new XDeclaration("1.0", "utf-8", "true");
                  XElement rootEle = new XElement("EventLog")
                  xml_document.Add(rootEle);

                  XElement event_node = new XElement("Event", new XAttribute("ID", Guid.NewGuid().ToString()));
                  XElement event_date = new XElement("DateTime", DateTime.UtcNow.ToString("ddd dd MMM yyyy HH:mm:ss"));
                  XElement event_details = new XElement("Description", event_description);
                  event_node.Add(event_date);
                  event_node.Add(event_details);

                  // Append element to root
                  rootEle.Add(event_node);

                  xml_document.Save("eventlog.xml");

                  /ravi

                  My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com

                  J 1 Reply Last reply
                  0
                  • R Ravi Bhavnani

                    Try this:

                    xml_document = new XDocument();
                    xml_document.Declaration = new XDeclaration("1.0", "utf-8", "true");
                    XElement rootEle = new XElement("EventLog")
                    xml_document.Add(rootEle);

                    XElement event_node = new XElement("Event", new XAttribute("ID", Guid.NewGuid().ToString()));
                    XElement event_date = new XElement("DateTime", DateTime.UtcNow.ToString("ddd dd MMM yyyy HH:mm:ss"));
                    XElement event_details = new XElement("Description", event_description);
                    event_node.Add(event_date);
                    event_node.Add(event_details);

                    // Append element to root
                    rootEle.Add(event_node);

                    xml_document.Save("eventlog.xml");

                    /ravi

                    My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com

                    J Offline
                    J Offline
                    Jassim Rahma
                    wrote on last edited by
                    #9

                    Thanks Ravi. It solved my problem.

                    Technology News @ www.JassimRahma.com

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups