Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing whether or not something is parseable XML in C# [duplicate]

Tags:

c#

.net

xml

Does anyone know of a quick way to check if a string is parseable as XML in C#? Preferably something quick, low resource, which returns a boolean whether or not it will parse.

I'm working on a database app which deals with errors that are sometimes stored as XML, and sometimes not. Hence, I'd like to just be able to test the string I grab from the database (contained in a DataTable) very quickly...and not have to resort to any try / catch {} statements or other kludges...unless those are the only way to make it happen.

like image 523
user978122 Avatar asked Sep 09 '13 18:09

user978122


People also ask

How check string is XML or not in C#?

Show activity on this post. string xml = ""; XDocument document = XDocument. Parse(xml); And if you don't want to have the ugly try/catch visible, you can throw it into an extension method on the string class...

How do you check if XML file is well formed in C#?

Simply run it through a parser. That will perform the appropriate checks (whether it parses ok). If it's a large document (as indicated) then an event-based parser (e.g. SAX) will be appropriate since it won't store the document in memory. It's often useful to have XML utilities around to check this sort of stuff.

How do I know if I have XML or JSON?

Very simple: Valid JSON starts always with '{' or '[' Valid XML starts always with '<'


2 Answers

It sounds like that you sometimes get back XML and sometimes you get back "plain" (non-XML) text.

If that's the case you could just check that the text starts with <:

if (!string.IsNullOrEmpty(str) && str.TrimStart().StartsWith("<"))
    var doc = XDocument.Parse(str);

Since "plain" messages seem unlikely to start with < this may be reasonable. The only thing you need to decide is what to do in the edge case that you have non-XML text that starts with a <?

If it were me I would default to trying to parse it and catching the exception:

if (!string.IsNullOrEmpty(str) && str.TrimStart().StartsWith("<"))
{
    try
    {
        var doc = XDocument.Parse(str);
        return //???
    }   
    catch(Exception ex)
        return str;
}
else
{
    return str;   
}

That way the only time you have the overhead of a thrown exception is when you have a message that starts with < but is not valid XML.

like image 189
D Stanley Avatar answered Sep 26 '22 23:09

D Stanley


You could try to parse the string into an XDocument. If it fails to parse, then you know that it is not valid.

string xml = "";
XDocument document = XDocument.Parse(xml);

And if you don't want to have the ugly try/catch visible, you can throw it into an extension method on the string class...

public static bool IsValidXml(this string xml)
{
    try
    {
        XDocument.Parse(xml);
        return true;
    }
    catch
    {
        return false;
    }
}

Then your code simply looks like if (mystring.IsValidXml()) {

like image 39
John Kraft Avatar answered Sep 26 '22 23:09

John Kraft