Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
There are two buttons. When I click first button the startdocument and the other contents will be created and then when I click second button it has to append the content and save the file . But when I click i get an error . "Invalid xml document and document needs a root element.

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace GenerateXmlVertwo
{
    public partial class Form1 : Form
    {

        static XmlDocument doc = new System.Xml.XmlDocument();
        XmlWriter writer = doc.CreateNavigator().AppendChild();

        public Form1()
        {
            InitializeComponent();

            writer.WriteStartDocument();
            writer.WriteStartElement("seleniumObjectRepository");
            writer.WriteStartElement("seleniumRepObjects");
            MessageBox.Show("Pritned start element");
        }


        public void Form1_Load(object sender, EventArgs e)
        {

        }

        public void button1_Click(object sender, EventArgs e)
        {
            //  System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
            //   using (XmlWriter writer = doc.CreateNavigator().AppendChild())
            // {
            // Do this directly 
            //  XmlWriterSettings xws = new XmlWriterSettings();

            writer.WriteStartElement("seleniumRepObject");
            writer.WriteAttributeString("Class", "WebElement");
            writer.WriteAttributeString("Name", "RegisterLink");
            writer.WriteStartElement("seleniumRepProperty");
            writer.WriteAttributeString("Name", "xpath");
            writer.WriteElementString("seleniumRepValue", "registerWithoutClaim");

            MessageBox.Show("printed the content");

            // }



        }

        public void button1_Click_1(object sender, EventArgs e)
        {

            try
            {

                writer.WriteEndElement();
                writer.WriteEndDocument();
                doc.Save(@"filelocation\vas.xml");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                
            }

        }


    }
}
Posted
Updated 13-May-15 18:44pm
v2
Comments
Sergey Alexandrovich Kryukov 14-May-15 1:05am    
In what line? What's unclear in the message?
—SA
ShaHam11 14-May-15 1:06am    
I get error message while doing doc.save(@fileLocation\..")
Sergey Alexandrovich Kryukov 14-May-15 10:24am    
All right, thank you. You already got a correct answer.
—SA

1 solution

The error message is quite understandable. Please, refer xml documentation: Extensible Markup Language (XML) 1.1 - documents[^]:
Quote:
[Definition: There is exactly one element, called the root, or document element, no part of which appears in the content of any other element.] For all other elements, if the start-tag is in the content of another element, the end-tag is in the content of the same element. More simply stated, the elements, delimited by start- and end-tags, nest properly within each other.


So, you need to add root element to your xml:
HTML
<somenameofrootelement>
    <seleniumobjectrepository></seleniumobjectrepository>
    <seleniumrepobjects></seleniumrepobjects>
</somenameofrootelement>


[EDIT]
I'd suggest you to change your logic...

I do not recommend you to create global objects (even if it's used only within form class). Rather than it, open, edit and save xml document within button click event.

See:
XmlReader Class[^]
How to: Parse XML with XmlReader[^]
XmlWriter Class[^]
Combining the XmlReader and XmlWriter classes for simple streaming transformations[^]

Final note:
The simplest way to operate on xml data is to use xml serialization and deserialization[^].
 
Share this answer
 
v2
Comments
ShaHam11 14-May-15 2:38am    
@Maciej Los .Thanks for the reply. The root element is added in form1 constructor ..It seems that when i click the second button (during click event ).. the root element vanishes...basically I needed only the content in button_click1 event to be repeated everytime
Maciej Los 14-May-15 2:51am    
So, debug the program and try to improve it. When you get stuck, come back here and post a comment.
ShaHam11 14-May-15 5:44am    
I tried debugging the errors is in while saving doc.save ...same message
Sergey Alexandrovich Kryukov 14-May-15 10:23am    
5ed.
—SA
Maciej Los 14-May-15 10:24am    
Thank you, Sergey ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900