Writing an easy xml

  • Thread starter Thread starter alberto
  • Start date Start date
A

alberto

I'd like to write a xml file like this:

<?xml version="1.0"?>
<a>
<b>
<value>abc</value>
</b>
<c>
<value>def</value>
</c>
</a>

I'm doing this:

XmlTextWriter tw = new XmlTextWriter(fileName, null);

tw.WriteStartDocument();

tw.WriteStartElement("b");
tw.WriteElementString("value", "abc");
tw.WriteEndElement();

tw.WriteStartElement("c");
tw.WriteElementString("value", "def");
tw.WriteEndElement();

tw.WriteEndDocument();

But in the second "WriteStartElement" fails. The compiler says that
StartElement is in the state "Epilog" and there will be an xml document
not valid.
Thank you.
 
alberto said:
I'd like to write a xml file like this:

<?xml version="1.0"?>
<a>
<b>
<value>abc</value>
</b>
<c>
<value>def</value>
</c>
</a>

I'm doing this:

XmlTextWriter tw = new XmlTextWriter(fileName, null);

tw.WriteStartDocument();

tw.WriteStartElement("b");
tw.WriteElementString("value", "abc");
tw.WriteEndElement();

tw.WriteStartElement("c");
tw.WriteElementString("value", "def");
tw.WriteEndElement();

tw.WriteEndDocument();

But in the second "WriteStartElement" fails. The compiler says that
StartElement is in the state "Epilog" and there will be an xml document
not valid.

You never created the root "a" node....
 
El 15/12/2009 22:18, Jeff Johnson escribió:
You never created the root "a" node....
Yes, but it isn't the problem. The problem is the second WriteStartElement.
 
I'd like to write a xml file like this:

<?xml version="1.0"?>
<a>
<b>
<value>abc</value>
</b>
<c>
<value>def</value>
</c>
</a>

I'm doing this:

XmlTextWriter tw = new XmlTextWriter(fileName, null);

tw.WriteStartDocument();

tw.WriteStartElement("b");
tw.WriteElementString("value", "abc");
tw.WriteEndElement();

tw.WriteStartElement("c");
tw.WriteElementString("value", "def");
tw.WriteEndElement();

tw.WriteEndDocument();

But in the second "WriteStartElement" fails. The compiler says that
StartElement is in the state "Epilog" and there will be an xml document
not valid.

As written the 'b' node will be the top level element. The 'c' node
also wants to be the top level element. If the code "worked" the
output would be something like this.

<?xml version="1.0"?>
<b>
<value>abc</value>
</b>
<c>
<value>def</value>
</c>

Your code never writes the 'a' node.
tw.WriteStartDocument();
tw.WriteStartElement("a");
tw.WriteStartElement("b");
tw.WriteElementString("value", "abc");
tw.WriteEndElement();

tw.WriteStartElement("c");
tw.WriteElementString("value", "def");
tw.WriteEndElement(); tw.WriteEndElement();

tw.WriteEndDocument();

regards
A.G.
 
El 15/12/2009 22:18, Jeff Johnson escribió:
Yes, but it isn't the problem. The problem is the second WriteStartElement.

The second WriteStartElement is the problem because there is no 'a'
node written to be the parent of 'b' and 'c' nodes.

Your example show the 'a' node as the document root. The code writes
the 'b' node as the document root. The second WriteElement call
attempts to add the 'c' node as the document root. An XML document can
have only one root (top level element) so an exception is thrown by
the second call to WriteStartElement.

Add a line of code to start the 'a' element and a line to end that
element. From my previous post.
tw.WriteStartDocument();
tw.WriteStartElement("a");
tw.WriteStartElement("b");
tw.WriteElementString("value", "abc");
tw.WriteEndElement();

tw.WriteStartElement("c");
tw.WriteElementString("value", "def");
tw.WriteEndElement(); tw.WriteEndElement();

tw.WriteEndDocument();

regards
A.G.
 
Back
Top