Xml validation - Should fail but does not

R

Raghu

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/..
 
M

Martin Honnen

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.
 
R

Raghu

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/..
 
R

Raghu

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/..
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top