Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Easily validating a String containing XML

Status
Not open for further replies.

DayLaborer

Programmer
Jan 3, 2006
347
US
Is there a way to more easily validate a string containing XML other than this:
Code:
XmlDocument doc = new XmlDocument();
doc.LoadXml(myStringContainingXML);
...and see if it throws an exception. The thing is, I don't need an XMLDocument object, I just need a String containing valid XML.

What I'd love to do is this:
Code:
bool isAllCool = XmlDocument.Validate(myStringContainingXML);
...but unfortunately such a static method does not exist.

Suggestions?

Thanks,
Eliezer
 
that is what you are doing with a xmldocument, the only difference is you are using an instance of an object rather than an static method. if you want you can create a static helper method
Code:
class XmlValidator
{
   public static bool Validate(string xml)
   {
       try
       {
           new XmlDocument().Load(xml);
           return true;
       }
       catch
       {
           return false;
       }
   }
}

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
There's really no way to test validity without planning to throw an exception?

Thanks,
Eliezer
 
the Load method parses the xml input.
if the parse failes you get an exception, that's how it works.
if you want to validate the xml, you need an XSD schema.

sample here :


/Daddy

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
using the XmlDocument.Load() only validates the xml is well formed. XSD would validate the nodes/attributes are correct.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
But even the link that Mr. Daddy provided still does that
Code:
document.Load(XMLFILEPATH);
wrapped in a try-catch block and relies on an exception being thrown.

I'd really like to get away from that.
Eliezer
 
what is wrong with try/catching the exception to validate the xml?

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
like I said, that is the way it works. no other way to do this.

The exception message will say what is wrong with the xml input.

/Daddy

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
Exceptions are slow and I need this function to run quickly. If there's no other way, though...
 
Exceptions are slow and I need this function to run quickly.
compared to... what? this is a subjective statement that doesn't really mean anything. if you say I need to process X number of xml strings within Y milliseconds; then you have a goal.

something else to consider is: will loading the xml for validation be the bottleneck? if not then any performance gain you might get from a different process is nullified by the true bottleneck.

once the application is testable run it through a profiler like DotTrace. This will provide concrete figures on what areas of the system need to be refactored.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
ah, now we are talking, you need a fast xml parser.
well the ms parser does ok, but there are faster alternatives out there :


google "fast xml parser" for more links :)

/Daddy



-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
Are those supposed to be faster than throwing / catching an exception?
 
only way to find out is to test, like Jason told you.

/Daddy

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top