0

I have many XML files and some of them might have unclosed strings like this

<ns0:Info InfoTyp="53" InfoID="/>

Those unclosed strings dont always appear as the last part of the tag

Is there a way in Notepad++ or in C# to easily detect when a file has this kind of strings ?

How can I also detect other kind of error in the XML file that make it an invalid XML ? I need to try to parse it to detect this ?

Sergey Berezovskiy
  • 224,436
  • 37
  • 411
  • 441

3 Answers3

1

With C# you can try to load xml file into XDocument (or XmlDocument):

using System.Xml.Linq; // include this in your using directives

try
{
    var xdoc = XDocument.Load(path_to_xml);
}
catch (XmlException e)
{
    // xml is invalid     
}

XmlException contains information about line number and position which caused error. Also exception message is pretty informative. E.g. for you xml it will say:

Unexpected end of file has occurred. The following elements are not closed: Line 1, position 35.

Cameron Tinker
  • 9,354
  • 10
  • 44
  • 82
Sergey Berezovskiy
  • 224,436
  • 37
  • 411
  • 441
0

In c#

try
{
    XDocument doc=XDocument.Load(path);
}
catch(XmlException ex)
{
    //oops xml not formatted properly
}

In Notepad++

Check RegularExpression option

Find What: \A([^"]*"[^"]*"[^"]*)*\z

If it matches you have a valid xml

Anirudha
  • 31,626
  • 7
  • 66
  • 85
0

You can validate against an XSD schema to ensure an XML document is well formed and conforms to a specific structure.

This is a good post to get you started if using .NET...

https://stackoverflow.com/a/2553468/1246574

Community
  • 1
  • 1
Jim
  • 6,555
  • 12
  • 42
  • 71
  • You can generate one easily enough, even using Visual Studio, or an XML editor like Oxygen. – Jim Sep 24 '13 at 16:05
  • 1
    Validation is different than checking if a document is well-formed. Unclosed strings would indicate a document that wasn't well-formed and should even be allowed to be validated. – James Cronen Sep 24 '13 at 16:21