Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Check XML to see if well-formed

Posted on 2007-11-27
2
Medium Priority
?
932 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 2000 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

Industry Leaders: 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

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 …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

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