Error creating XML with Xmlwriter

Asked

Viewed 94 times

1

I need to create an XML, but when I try to create 2 error elements, how do I generate 1 XML with two elements? follows my code

class Program
{
    static void Main(string[] args)
    {

        using (XmlWriter writer = XmlWriter.Create("books.xml"))
        {
            writer.WriteStartElement("book");
            writer.WriteElementString("title", "Graphics Programming using GDI+");
            writer.WriteElementString("author", "Mahesh Chand");
            writer.WriteElementString("publisher", "Addison-Wesley");
            writer.WriteElementString("price", "64.95");
            writer.WriteEndElement();

            writer.WriteStartElement("newBook");
            writer.WriteElementString("title", "Graphics Programming using GDI+");
            writer.WriteElementString("author", "Mahesh Chand");
            writer.WriteElementString("publisher", "Addison-Wesley");
            writer.WriteElementString("price", "64.95");
            writer.WriteEndElement();
            writer.Flush();
        }
    }
}

Error code:

System.Invalidoperationexception: 'Token Startelement in state Endrootelement would result in an invalid XML Document. Make sure that the Conformancelevel Setting is set to Conformancelevel.Fragment or Conformancelevel.Auto if you want to write an XML Fragment. '

  • Check out this link for some examples of how to do http://www.macoratti.net/12/09/xml_cur2.htm

1 answer

3

You need to define the ROOT element - it is the root element of your tree. The first element you should create, will be just this knot.

writer.WriteStartElement("root");

Your complete code:

class Program
{
    static void Main(string[] args)
    {

        using (XmlWriter writer = XmlWriter.Create("books.xml"))
        {
            writer.WriteStartElement("root");

            writer.WriteStartElement("book");
            writer.WriteElementString("title", "Graphics Programming using GDI+");
            writer.WriteElementString("author", "Mahesh Chand");
            writer.WriteElementString("publisher", "Addison-Wesley");
            writer.WriteElementString("price", "64.95");
            writer.WriteEndElement();

            writer.WriteStartElement("newBook");
            writer.WriteElementString("title", "Graphics Programming using GDI+");
            writer.WriteElementString("author", "Mahesh Chand");
            writer.WriteElementString("publisher", "Addison-Wesley");
            writer.WriteElementString("price", "64.95");
            writer.WriteEndElement();
            writer.Flush();
        }
    }
}
  • 1

    @Marcelohenriquedosreis Perhaps because it is a collection of <book> it would be interesting to call root <books> and not possess two types of objects within it. Instead of <book> and <newBook>, you should have just <book> and or add an element <newBook>true</newBook> or only the attribute <book newBook="true">.

  • That’s right Leandro, the Books knot would be more appropriate. As for the repetition of objects, I believe that the author of the question only posted as an example with ctrlC + ctrlV.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.