473,387 Members | 1,420 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.

Xml validation - Should fail but does not

I have following schema:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema
elementFormDefault="qualified"
targetNamespace="http://mycompany.services.customer2/types/restricted"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:tns="http://mycompany.services.customer2/types/restricted">

<xs:simpleType name="FirstName">
<xs:restriction base="xs:string">
<xs:minLength value="5" />
<xs:maxLength value="10" />
</xs:restriction>
</xs:simpleType>
<xs:complexType name="CustomerSearchInfo2">
<xs:sequence>
<xs:element name="FirstName" type="tns:FirstName" />
<!--<xs:element name="FirstName" type="xs:string" />-->
</xs:sequence>
</xs:complexType>
<xs:element name="searchInfo2" type="tns:CustomerSearchInfo2"/>
</xs:schema>

According to the above schema, following xml should be invalid:

<searchInfo2 xmlns="http://mycompany.services.customer2/types/restricted">
<!-- First name is less than minimum length -->
<FirstName>John</FirstName>
</searchInfo2>

Here is the code I am using:
private void ReadXml(XmlReader reader)
{
XmlReader thisReader = null;
m_validationErrors = new StringBuilder();

if (m_useSchemaValidation)
{
thisReader = GetSchemaValidatingReader(reader);
thisReader.Read(); //Read CustomerSearchInfo2 element
}
else
{
thisReader = reader;
}

thisReader.Read(); //Read first child element which is
FirstName (If invalid, fires the validation event)

//Start reading child elements
string firstName = null;

if (m_validationErrors.Length == 0) //Check for validationg
errors
{
if (reader.IsStartElement("FirstName", m_namespace))
{
if (thisReader.Read() && thisReader.NodeType ==
XmlNodeType.Text)
{
firstName = reader.Value;
Console.WriteLine("FirstName: {0}", firstName);
}
else
{
m_validationErrors.Append("The FirstName element
does not contain text value."
+ Environment.NewLine);
}
}
else
{
m_validationErrors.Append(
string.Format("First child element should be
FirstName in name space {0}.", m_namespace)
+ Environment.NewLine);
}
}

//Final check to get all the validation rules in place.
if (m_validationErrors.Length 0)
{
Console.WriteLine("Xml contenet is invalid according to
schema. Details: {0}", m_validationErrors.ToString());
}
else
{
Console.WriteLine("Xml contenet is valid according to
schema.");
}
}

private XmlReader GetSchemaValidatingReader(XmlReader reader)
{
XmlReaderSettings settings = new XmlReaderSettings();
settings.XmlResolver = new XmlUrlResolver();

StreamReader streamReader = new StreamReader(m_schemaFilePath);
XmlSchema schema = XmlSchema.Read(streamReader.BaseStream,
null);

settings.Schemas.Add(schema);

settings.Schemas.Compile();
settings.ValidationType = ValidationType.Schema;

settings.ValidationEventHandler += delegate(object sender,
ValidationEventArgs e)
{
m_validationErrors.Append(e.Message + Environment.NewLine);
};

settings.IgnoreComments = true;
settings.IgnoreWhitespace = true;

XmlReader validatingReader = XmlReader.Create(reader, settings);
return validatingReader;
}

How can I have the reader fire the validation error on the FirstName
content?

Thanks.
Raghu/..
Jul 13 '06 #1
3 2005


Raghu wrote:

thisReader.Read(); //Read first child element which is
FirstName (If invalid, fires the validation event)
Where does that assumption come from? If the start tag of the element
has been read the reader will not be able to fire validation errors on
the content of the element that has not yet been read.
For your example XML the reader first has to read the text node inside
the element to be able to give the validation error.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Jul 13 '06 #2
Hi Martin,

It is not an assumption but a fact. Here is how I found it. I gave FirstName
element a different namespace. This particular statement caused the
validation event to be fired.

Any way, I am still interested in why the validation event is not firing for
the content of the FirstName element as it is invalid according to the
schema. Interestingly, the VS.net ide shows the errors while I authored the
xml...

Thanks for the input.

Raghu/..

"Martin Honnen" <ma*******@yahoo.dewrote in message
news:OW****************@TK2MSFTNGP05.phx.gbl...
>

Raghu wrote:

> thisReader.Read(); //Read first child element which is
FirstName (If invalid, fires the validation event)

Where does that assumption come from? If the start tag of the element has
been read the reader will not be able to fire validation errors on the
content of the element that has not yet been read.
For your example XML the reader first has to read the text node inside the
element to be able to give the validation error.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/

Jul 13 '06 #3
Figured out the problem:

The following code:
if (thisReader.Read() && thisReader.NodeType ==
XmlNodeType.Text)
{
firstName = reader.Value;
Console.WriteLine("FirstName: {0}", firstName);
}
else
{
m_validationErrors.Append("The FirstName element
does not contain text value."
+ Environment.NewLine);
}
should simply be replaced by:

firstName = reader.ReadElementContentAsString();

This causes the validation event to be fired with an error....

Raghu/..
"Raghu" <RaghuNoSpamwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
>I have following schema:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema
elementFormDefault="qualified"
targetNamespace="http://mycompany.services.customer2/types/restricted"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:tns="http://mycompany.services.customer2/types/restricted">

<xs:simpleType name="FirstName">
<xs:restriction base="xs:string">
<xs:minLength value="5" />
<xs:maxLength value="10" />
</xs:restriction>
</xs:simpleType>
<xs:complexType name="CustomerSearchInfo2">
<xs:sequence>
<xs:element name="FirstName" type="tns:FirstName" />
<!--<xs:element name="FirstName" type="xs:string" />-->
</xs:sequence>
</xs:complexType>
<xs:element name="searchInfo2" type="tns:CustomerSearchInfo2"/>
</xs:schema>

According to the above schema, following xml should be invalid:

<searchInfo2 xmlns="http://mycompany.services.customer2/types/restricted">
<!-- First name is less than minimum length -->
<FirstName>John</FirstName>
</searchInfo2>

Here is the code I am using:
private void ReadXml(XmlReader reader)
{
XmlReader thisReader = null;
m_validationErrors = new StringBuilder();

if (m_useSchemaValidation)
{
thisReader = GetSchemaValidatingReader(reader);
thisReader.Read(); //Read CustomerSearchInfo2 element
}
else
{
thisReader = reader;
}

thisReader.Read(); //Read first child element which is
FirstName (If invalid, fires the validation event)

//Start reading child elements
string firstName = null;

if (m_validationErrors.Length == 0) //Check for validationg
errors
{
if (reader.IsStartElement("FirstName", m_namespace))
{
if (thisReader.Read() && thisReader.NodeType ==
XmlNodeType.Text)
{
firstName = reader.Value;
Console.WriteLine("FirstName: {0}", firstName);
}
else
{
m_validationErrors.Append("The FirstName element
does not contain text value."
+ Environment.NewLine);
}
}
else
{
m_validationErrors.Append(
string.Format("First child element should be
FirstName in name space {0}.", m_namespace)
+ Environment.NewLine);
}
}

//Final check to get all the validation rules in place.
if (m_validationErrors.Length 0)
{
Console.WriteLine("Xml contenet is invalid according to
schema. Details: {0}", m_validationErrors.ToString());
}
else
{
Console.WriteLine("Xml contenet is valid according to
schema.");
}
}

private XmlReader GetSchemaValidatingReader(XmlReader reader)
{
XmlReaderSettings settings = new XmlReaderSettings();
settings.XmlResolver = new XmlUrlResolver();

StreamReader streamReader = new StreamReader(m_schemaFilePath);
XmlSchema schema = XmlSchema.Read(streamReader.BaseStream,
null);

settings.Schemas.Add(schema);

settings.Schemas.Compile();
settings.ValidationType = ValidationType.Schema;

settings.ValidationEventHandler += delegate(object sender,
ValidationEventArgs e)
{
m_validationErrors.Append(e.Message + Environment.NewLine);
};

settings.IgnoreComments = true;
settings.IgnoreWhitespace = true;

XmlReader validatingReader = XmlReader.Create(reader,
settings);
return validatingReader;
}

How can I have the reader fire the validation error on the FirstName
content?

Thanks.
Raghu/..


Jul 13 '06 #4

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

Similar topics

67
by: Scott Meyers | last post by:
I have a web site that, due to maintenance by several people, some of whom are fairly clueless about HTML and CSS, etc. (notably me), has gotten to the point where I'm pretty sure it's suffering...
41
by: Gérard Talbot | last post by:
Cross-posted to: comp.infosystems.www.authoring.html and alt.html Followup-to: comp.infosystems.www.authoring.html 1- One day, I stumbled across a website that offers to validate webpages. What...
16
by: Hosh | last post by:
I have a form on a webpage and want to use JavaScript validation for the form fields. I have searched the web for form validation scripts and have come up with scripts that only validate...
0
by: THY | last post by:
Hi, How do we raise an validation fail of a validation inside a sub ? the validation is valid, but I just want to raise the validation fail ... thanks
1
by: dhurwitz | last post by:
Hi I have developed a web app with a dozen or so pages, which allows searching for and editing orders by OrderID. There is a search page for entering multiple search criteria. In addition, each...
2
by: Shathish | last post by:
Ha i have this problem with the validation control I have a asp.net page which is used to add and edit projects when i'm adding a project the validation controls are working fin but if i select a...
1
by: Stephen Adam | last post by:
Hi there, I have written a custom validation control which checks to see of an input field is not empty and contains only numeric data. I was using a regular expression validation control but...
3
by: Raghu | last post by:
I have following schema: <?xml version="1.0" encoding="utf-8"?> <xs:schema elementFormDefault="qualified" targetNamespace="http://mycompany.services.customer2/types/restricted"...
6
by: lists | last post by:
Hi all, I am trying to validate an XML file against an XSD schema file within a ..NET C++ program, but the validation doesn't seem to be occuring. My code is listed below. The validation...
1
by: Parsed Cheese | last post by:
After two days I am at wits end, but I am too OCD'ed to give up. Please tell me what I have got wrong because no Validation Error is being thrown. The element: "<datestamp>I should...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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.