0

I have an xml string ,While de serializing this xml I got an error like 'There is an error in XML document (498, 31)' .How can I get the xml node at this position in c#,so that I can send it to user that there is an issue in this particular node.

using (TextReader reader = new StringReader(xml)) 
{ 
    try 
    { 
        tempClass = (T)new XmlSerializer(typeof(T)).Deserialize(reader); 
    } 
    catch (InvalidOperationException ex) 
    { 
        //Here we need to show the node in which the error occurred 
    } 
}

Here in catch I got the message like 'There is an error in XML document (498, 31)'.I want to throw a custom error message to the user that, 'in this particular 'node' there is an issue' Any help or ideas on the subject would be greatly appreciated.

klashar
  • 2,519
  • 2
  • 28
  • 38
Anuja
  • 59
  • 7
  • 6
    I use notepad++ for this, but I guess that's not what you're asking :) – slawekwin Feb 15 '17 at 08:02
  • It would be very helpful if you could show the relevant code and possibly part of the xml file as well as the exact error message – Dave S Feb 15 '17 at 08:02
  • 2
    There may not *be* an "xml" node at that position. That's the problem. It;s run into something that isn't valid xml. – Damien_The_Unbeliever Feb 15 '17 at 08:04
  • 3
    If the XML can't be deserilize then you can't use the XML terms and ask for "Node". You can open the file in any editor (or Visual Studio) and go to that line. – elirandav Feb 15 '17 at 08:05
  • @slawekwin : I want to get the node in my c# code, so that I can pass this node information to the user. Thank You. – Anuja Feb 15 '17 at 08:28
  • do you load the xml from a file? why not open it as a normal text file, and [skip](http://stackoverflow.com/questions/8037070/whats-the-fastest-way-to-read-a-text-file-line-by-line) to the line exception gives you (or a few before to give some context as well) – slawekwin Feb 15 '17 at 08:31
  • @slawekwin :This is my code, i want to get the node information in catch block. using (TextReader reader = new StringReader(xml)) { try { class= (T)new XmlSerializer(typeof(T)).Deserialize(reader); } catch (InvalidOperationException ex) { //Here we need to show the node in which the error occurred } } – Anuja Feb 15 '17 at 08:39
  • 1
    Why the negative votes? He's not asking why the XML file is invalid - if he did, then he should produce his code and the XML file, but that's not what he's asking. He's asking how to get a particular line from the XML file, which is a perfectly-valid question. – Zesty Feb 15 '17 at 08:39
  • @Anuja Please don't dump code as a comment. Instead edit your question. – Manfred Radlwimmer Feb 15 '17 at 08:50

1 Answers1

4

You can't use XML functions (as the file isn't valid XML), so read it as text and send the user the offending line.

string[] xmlLines = File.ReadAllLines(path);    
int linesFrom = 5;
int exceptionLine = 10; //Your  line number
int startLine = exceptionLine - linesFrom - 1 > 0 ? exceptionLine - linesFrom - 1: 0;
int endLine = exceptionLine + linesFrom - 1 > xmlLines.Count - 1 ? exceptionLine + linesFrom  - 1: xmlLines.Count - 1;
StringBuilder sb = new StringBuilder();
for (int i = startLine ; i < endLine ; i++)
{
    sb.Append(xmlLines[i]);
}
return sb.ToString();
Zesty
  • 2,922
  • 9
  • 38
  • 69
  • 2
    Maybe also include some surrounding lines, e.g. 497-5 up to 497+5 to have enough context. – Uwe Keim Feb 15 '17 at 08:42
  • does the exception contain the line number explicitly or do you need to parse it from the text? if so, how do you do that in case of different languages? – slawekwin Feb 15 '17 at 08:43
  • @Anuja Do you have an additional requirement to read the file from a web server? If so, ask that as a separate question. On this site, you should ask each question as a minimal, specific question. – Zesty Feb 15 '17 at 11:52