Solved

Check XML to see if well-formed

Posted on 2007-11-27
2
917 Views
Last Modified: 2012-08-13
Is there a way to check if a string containing XML contains well-formed XML?  I do not have, nor do I want an XSD.  Also, It is not an XML file, but rather an XML stub.

What I am doing is writing a function that will insert data into a SQL Server table.  One of the table's fields is of type xml.  If I do an insert and the xml field does not contain well-formed xml, then the insert throws an exception.  I'd like to catch this before I do the insert.

Also, note that I will have no control over how the XML string may be formed prior to it being passed to me.

Any ideas?

Thanks,
Chris
0
Comment
Question by:chlade
2 Comments
 
LVL 7

Accepted Solution

by:
sj_hicks earned 500 total points
ID: 20363321
The way I do it is to load it into an XMLDocument and try to access the properties.  An exception will be thrown if the XML is invalid.  e.g.:  (don't forget to imports system.xml)

dim xmldoc as new xmldocument
xmldoc.loadxml(strXMLdata)
try
  dim strTemp
  strTemp = xmldoc.outerxml
  ' if no exception throw, xml should be well formed
catch
  msgbox "invalid XML"
end try
0
 

Author Comment

by:chlade
ID: 20364851
Thanks.  It actually threw an exception when doing the LoadXml.  So I just moved that inside the Try/Catch and removed the outerxml line.  This seems to accomplish the same thing.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now