<?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.
You never created the root "a" node....
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.
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.
>> You never created the root "a" node....
>>
>>
> Yes, but it isn't the problem.
No, it is EXACTLY the problem!