Solved

Check XML to see if well-formed

Posted on 2007-11-27
2
926 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

829 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