Solved

Check XML to see if well-formed

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
get row value in vb.net 4 24
Help with exporting to excel 4 44
VB.NET String Settings and Temp Folder Question 3 68
Passing data between Forms 3 26
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

680 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