-1

In C#:

I have a set of rows of text (that should be XML) that are sent to a function to be added to an XML document. Each row is a simple block like these 3 examples:

<AAA f="1" g="3"> </AAA>
<AAA f="2" g="2"> </AAA>
<AAA f="3" g="3"> </AA     <-- Oops a bad one!

I need to check each row to see if it is valid XML before I accept it into a full XML document as some rows are bad, but we want all the good ones.

ROW1: good
ROW2: good
ROW3: bad
ROW4: good

In this case we need ROW1,2 and 4 only.

So, how do I check to see if the row string is valid XML when it is not a full document?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Ian Vink
  • 66,960
  • 104
  • 341
  • 555
  • 2
    wrap it in a document? – pm100 Feb 12 '13 at 18:01
  • How about reading the whole xml in XDocument or XmlDocument and showing the line numbers from the exception you'd get? You can apply that to single lines as well but it would be slow – Sten Petrov Feb 12 '13 at 18:01
  • All the "valid" examples you've given would be valid as a complete document, so you could parse them as complete documents. Can you give an example where that wouldn't be applicable? – Jon Skeet Feb 12 '13 at 18:02
  • There are suggestions in http://stackoverflow.com/questions/86292/how-to-check-for-valid-xml-in-string-input-before-calling-loadxml and http://stackoverflow.com/questions/1026247/check-well-formed-xml-without-a-try-catch; effectively, they all agree - you have to try and load the snippet into a document and catch the resultant exception for invalid XML snippets. – dash Feb 12 '13 at 18:14

3 Answers3

4

I'd try something like this:

public static bool IsValid(this string XML)
{
    try
    {
        XElement temp = XElement.Parse(XML);
    }
    catch (FormatException)
    {
        return false;
    }
    catch (XmlException)
    {
        return false;
    }
    return true;
}
It'sNotALie.
  • 22,289
  • 12
  • 68
  • 103
0

You can validate XML document fragments (that's what your lines are) with the XmlTextReader but you have to create the reader with special reader settings


XmlParserContext context = new XmlParserContext(null, nsmgr, null, XmlSpace.None);
XmlReaderSettings settings = new XmlReaderSettings() {
    ConformanceLevel = ConformanceLevel.Fragment
};
XmlReader reader = XmlReader.Create(new StringReader(xmlFrag), settings, context);

XmlReaderSettings ConformanceLevel

CSharper
  • 524
  • 3
  • 9
  • @John Saunders Nope, the constructor is not Deprecated, no ObsoleteAttribute. The docs just say that the recommended way is to use static XmlReader.Create method with appropriate XmlReaderSettings... But agreed: there's more power in that factory method. – CSharper Feb 12 '13 at 19:33
-3

i would do it like this.

private static bool IsValidXml(string xmlToCheck)
        {
            var doc = new XmlDocument();
            try
            {
                doc.LoadXml(xmlToCheck);
                return true;
            }
            catch (Exception ex)
            {
                //catch ex.
                return false;
            }
        }