Solved

Check XML to see if well-formed

Posted on 2007-11-27
2
928 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

739 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