473,387 Members | 1,585 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Validating a string is in Xml format without doing a try{ XmlDocument.Load(.......

The title says it all.

Is there a speed efficient way of validating that a string contains
valid Xml without
the overhead of try catch around a load attempt?

Thanks in advance.

May 10 '07 #1
4 7990
Paul wrote:
Is there a speed efficient way of validating that a string contains
valid Xml without
the overhead of try catch around a load attempt?
Reading your string with markup through an XmlReader (over a
StringReader) checks for well-formedness, if you want to validate too
you need to use XmlReaderSettings and set up the schemas first. There is
no need to use XmlDocument, you only need that if you want to manipulate
and change your XML.
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
May 10 '07 #2
Martin,

Here is what we have so far

string sValue = propertyInfo.GetValue(souceObject, null)
as string;

// Now we need to determine if it is in a format for
loading.

XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationType = ValidationType.Auto;
settings.ValidationFlags |=
System.Xml.Schema.XmlSchemaValidationFlags.None;
settings.ValidationEventHandler += new
System.Xml.Schema.ValidationEventHandler(settings_ ValidationEventHandler);

XmlReader reader = XmlReader.Create( new
MemoryStream( Encoding.Default.GetBytes( sValue ) ), settings);

while (reader.Read()) ;

if (isValidXml == false)
{
failureMessage = string.Format("Invalid Xml provided
in {0}", propertyInfo.Name);
}

return isValidXml;

But this still raises an exception.
The string it is attempting to validate is "rubbish".

This does not cause the reader to hit the validation handler.
But instead throws an unhandled exception.

May 10 '07 #3
Paul wrote:
string sValue = propertyInfo.GetValue(souceObject, null)
as string;

// Now we need to determine if it is in a format for
loading.

XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationType = ValidationType.Auto;
settings.ValidationFlags |=
System.Xml.Schema.XmlSchemaValidationFlags.None;
settings.ValidationEventHandler += new
System.Xml.Schema.ValidationEventHandler(settings_ ValidationEventHandler);

XmlReader reader = XmlReader.Create( new
MemoryStream( Encoding.Default.GetBytes( sValue ) ), settings);

while (reader.Read()) ;

if (isValidXml == false)
{
failureMessage = string.Format("Invalid Xml provided
in {0}", propertyInfo.Name);
}

return isValidXml;

But this still raises an exception.
The string it is attempting to validate is "rubbish".

This does not cause the reader to hit the validation handler.
But instead throws an unhandled exception.

Your attempt is not clear to me.

If you want to check for well-formedness only then you do not need any
validation event handler, you simply need to set up the reader and have
that while loop reading through. It will throw an exception however if
the markup is not well-formed so avoiding an exception is not possible
as far as I see it, what you can avoid is using XmlDocument, you only
need an XmlReader.

If you want to perform a validation then that only makes sense if you
provide a schema or several schemas as validation is done against a
schema or schema set. In that case you need to a validation event
handler which will then be called for every validation error but not for
well-formedness errors.

And there is no need to use memory streams and encodings, the XmlReader
can deal with all of that much better than your above code, you can
simply do e.g.
using (XmlReader xmlReader = XmlReader.Create(new StringReader(sValue)))
try {
while (xmlReader.Read()) {}
}
catch (XmlException e) {
// deal with error here
}
}

I hope that is clearer now, you need to distinguish between
well-formedness checks and validation.
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
May 10 '07 #4
Thanks Martin,

We have gone with the Exception handler.

The problem we were trying to solve was validating a property of type
string.
That property should contain a value that is in Xml i.e. <myXml>value</
myXmland NOT "my string"

As this validation class operates on the business object layer and is
intended primarily as layer
of developer validation for UI developers it is I think ok to use a
try catch.

Thanks for trying though.

May 10 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: KathyB | last post by:
Hi again, I save a fairly small xmlDocument to a single db field, xDoc.OuterXML using a SQL insert statement...this works fine. However, I then need to select that field (xmlWF) into an...
6
by: Robert Reineri | last post by:
Hello, New to the XML world and .NET. I have what I believe to be a simple problem, but I have read the .NET docs till I'm blue in the face and still can't locate a simple example of how to...
1
by: Craig Beuker | last post by:
Hello, I am experimenting with this XmlValidatingReader and have a question about how it is working (or not working as would be the case) The sample documents and code are included at the end...
2
by: Ian | last post by:
I've inherited some xml that doesn't have a namespace and doesn't have a reference to the schema. The schema doesn't have a targetNamespace attribute. I'm trying to build a test tool, in C#, which...
2
by: josh | last post by:
Hi, I am trying to validate cXML documents against cXML.dtd using the XmlValidatingReader. If I set the XMLValidatingReader's ValidatingType to ValidationType.DTD, I get the following...
0
by: delphiconsultingguy | last post by:
Hi all, Spent WAAAYYY too much time trying to figure this out because there's not many good examples out there, so in the interest of sparing y'all from suff'rin same, I've pasted it into...
5
by: th3dude | last post by:
I've searched quite a bit for this one and i can't find any solid solution for what i'm doing here. Right now i'm geting an xml string from an API, creating an xml file, then read the file with...
1
by: Chris Lieb | last post by:
I have an XML Schema file that I know is correct becuase I currently use it in a VB6 program to validate XML documents. Also, if I load an XML file into VS2005 that is not valid against this...
4
by: agda.karlberg | last post by:
Hello, I need to remove the DTD reference from an xml document, the reason for this is that we want to validate against a schema instead (which we have locally). It takes up to a minute to fetch...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.