tags:

views:

404

answers:

3

In C#, is there a way to work out an XmlNode's position in the original XML 'text', when the document is loaded from a file or string? I want to be able to report problems with an XML document that I am processing.

e.g:

"Error in foo.xml - value of attribute 'pet' must be a species of fluffy mammal, at line 27, column 13 [snippet of original XML text here...]"

Edit: The checks can't be done using schema validation. Here is another, less frivolous sample error message to illustrate: "specified addin type 'Addins.LogWindow' must be public"

+1  A: 

Would a XML Schema work for you?

http://support.microsoft.com/kb/318504

Sergio
I am also initially validating against a schema, but the checks I need to do can't be done with an XSD.
mackenir
+1  A: 

Sorry, there are very few DOM implementations that will remember the original parsed location of a Node for you. Most only report any position information on a parsing error. For example in DOM Level 3 LS you only get a reference to a DOMLocator when there is a DOMError.

The only imp I know of that keeps track after parsing is pxdom, and that's for Python so not of much use to you.

bobince
+2  A: 

Well you're not supposed to write your own XmlParser but in the Compact Framework we have no choice as XmlDocument is as slow as the Dalai Lama on ketamine so we use an XmlReader when parsing an Xml file.

We throw an exception whenever we find something messed up or inconsistent and we pass the XmlReader to the exception. We then can extract the line position by casting the XmlReader into a IXmlLineInfo object which contains properties for the line and position.

Don't know if this will help. Generally I wouldn't be writing my own XmlParser on desktop which is why im reticent to suggest this as a solution.

Quibblesome
Aha. I'm thinking, given the position of an XmlNode relative to its siblings and parent (and so on up to the root element) I can use an XmlTextReader to locate where that element is in the file.i.e: the 3rd element of the 2nd element of the root element -> read to this with XmlTextReader, get pos.
mackenir
Well I was thinking more if you were using the XmlReader to parse in the first place. What objects are you using to parse and validate the xml? Is it XmlDocument?
Quibblesome
Oh no... XmlReader is too complicated to use to actually read XML files :). I got my scheme working - the easy job of parsing the doc is done with XmlDocument, and then to get the location of an element I use XmlTextReader. I'll update this Q with an A at some point - it's a 'good enough' soln.
mackenir
XmlReader ain't too complicated to read xml files! You just have to think about things a little differently. Once you have the pattern down its fine :).
Quibblesome
It's a bit too low level for me, for this task. I can use XmlDocument and do XPath queries, etc. Also, I find that you need to try stuff out to see what is actually iterated over, as the docs are not comprehensive.
mackenir