473,394 Members | 1,944 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,394 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 1448


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...
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: 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...
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.