During XML exception processing, how do I get the line number in the XML file that's causing the exception?

Viewed 517

This question might sound similar to other question in Stack Overflow, but is different.

I am trying to load an XML file using new XmlDocument() & LoadXml(). While doing so there are some xml tags that are incomplete or incorrect and due to it is throwing exception. I want to make a list of them. Now I want this line number from exception object:

enter image description here

I want to extract line number pointed with RED arrow (which I thought would be ex.LineNumber, but it is not). I can extract from ex.Message (highlighted with YELLOW), but if it is available (like the place marked with RED arrow) then I would like to figure out how to get that value. Or is it not possible?

More clarification: Also if you look at StackTrace then you might find that that is another line number (Line 47) mentioned in the end but that's not what I am looking for (shown below).

   at System.Xml.XmlTextReaderImpl.Throw(Exception e)
   at System.Xml.XmlTextReaderImpl.Throw(String res, String[] args)
   at System.Xml.XmlTextReaderImpl.ThrowTagMismatch(NodeData startTag)
   at System.Xml.XmlTextReaderImpl.ParseEndElement()
   at System.Xml.XmlTextReaderImpl.ParseElementContent()
   at System.Xml.XmlTextReaderImpl.Read()
   at System.Xml.XmlLoader.LoadNode(Boolean skipOverWhitespace)
   at System.Xml.XmlLoader.LoadDocSequence(XmlDocument parentDoc)
   at System.Xml.XmlLoader.Load(XmlDocument doc, XmlReader reader, Boolean preserveWhitespace)
   at System.Xml.XmlDocument.Load(XmlReader reader)
   at System.Xml.XmlDocument.LoadXml(String xml)
   at <ProjectName>.Program.EvaluateEpisodeList(String strSeriesHomePageCode) in C:\<Path>\<ProjectName>\Program.cs:line 47

Hope this clarifies my question.

Most of the similar question on internet is for extracting line number from StackTrace, I didn't find anything that would solve my problem.

Thanks in advance.

2 Answers

Edit: I have completely misunderstood your question. here is the answer:


Catch an XmlException instead of a general exception. Then you can get the line number with e.LineNumber.

MSDN has this to say on LineNumber (my emphasis):

Gets the line number indicating where the error occurred. Line numbers start at 1.

Attempting to load the string "<\r\nbar" as XML throws an XmlException with the following message:

System.Xml.XmlException: 'Name cannot begin with the '<' character, hexadecimal value 0x3C. Line 1, position 2.'

Showing that line number is 1-based (the error is found in the first line). Tell me more...

Like this:

try
{
    // Load XML here
} 
catch (XmlException e) // <-----
{
    Console.WriteLine("Line: " + e.LineNumber);
}

More

Here is the documentation, you have probably found it already:

maybe it can be useful..

Exception class and properties

or

catch (XmlException ex) 
{
    Console.WriteLine("Line: "+ex.LineNumber);
//or
    MessageBox.Show($"Line: {ex.LineNumber}")
}
Related