Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Writing an easy xml

0 views
Skip to first unread message

alberto

unread,
Dec 15, 2009, 3:09:19 PM12/15/09
to
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.

Jeff Johnson

unread,
Dec 15, 2009, 4:18:02 PM12/15/09
to
"alberto" <alb...@nospam.es> wrote in message
news:OsYwDKcf...@TK2MSFTNGP04.phx.gbl...

You never created the root "a" node....


Registered User

unread,
Dec 15, 2009, 5:54:56 PM12/15/09
to

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.

Alberto

unread,
Dec 15, 2009, 5:54:40 PM12/15/09
to
El 15/12/2009 22:18, Jeff Johnson escribi�:
Yes, but it isn't the problem. The problem is the second WriteStartElement.

Registered User

unread,
Dec 15, 2009, 7:03:25 PM12/15/09
to

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.

Jeff Johnson

unread,
Dec 16, 2009, 9:17:49 AM12/16/09
to
"Alberto" <alb...@nospam.es> wrote in message
news:OlW5Ymdf...@TK2MSFTNGP06.phx.gbl...

>> You never created the root "a" node....
>>
>>
> Yes, but it isn't the problem.

No, it is EXACTLY the problem!


0 new messages