Welcome to Geekdojo Sign in | Join | Help

My first silly XML mistake

My first silly XML mistake, perhaps this is one of those things that are labeled as 'classic' (like '=='& '=' in C/C++)

I needed to capture the data that I was sending to a web service and see what it looked like, so, the best way that seemed interesting also was to write out the data as XML into a file. Only thing is that I had never used XML writers before, I just kind of understand what XML is for, so I looked at some code and also looked into the reference (SDK) for the related classes (XmlTextWriter) and important methods (WriteStartElement, WriteStartString, WriteStartAttributeString..) and started out.

But it was throwing error at some point, and I was totally foxed for about 10 minutes, then suddenly I realised that all well formed XML should have a single root element, I started the document, started an element ended it and was starting and element again, this is where it was throwing the error.

here is an extract :-
writer.WriteStartDocument();
    writer.WriteStartElement("New Company Details");
        writer.WriteString("MSFT");
    writer.WriteEndElement("New Company Details");
writer.WriteStartElement("New Location Details"); // <- here it was throwing the exception
....
(perhaps this could be made into an interview question, what is wrong with the above ?? ;-))

I added the root element to it and the mistake was corrected.
Xml Pearl No. 1 (Aha!!)

Published Wednesday, November 19, 2003 7:46 PM by richardhsu
Filed Under:

Comments

Anonymous comments are disabled