Xml Schema validations and namespaces...

D

Dan Bass

There's an XML message I have, that has no namespace information.
Then there is a XSD schema that is must validate against, but this has a
targetNamespace and xmlns of "http://www.wbf.org/xml/b2mml-v02".
How do I get this XML to validate against the Schema in C#?

If I use XmlSpy (2005 home edition) to perform the validation, it first
inserts namespace and schema information into the XML before validating.
Validation then seems to work if I take this modified XML and push it
through my code, only problem is none of my XSL mappings now work because
they don't include any namespace information!


---------------------------------------------------------------------------
I've listed the relevant code below:


I've got the XML:

<?xml version="1.0" encoding="UTF-8"?>
<ProductionSchedule>
<Location>
<EquipmentID>0945</EquipmentID>
<EquipmentElementLevel>Site</EquipmentElementLevel>
</Location>
<PublishedDate>2004-12-08T13:20:27.804</PublishedDate>
<ProductionRequest>
<ID>000010002377</ID>
<SegmentRequirement>
<ID>100000000000002689</ID>
<globe_SegmentState>Finished</globe_SegmentState>
</SegmentRequirement>
</ProductionRequest>
</ProductionSchedule>


Which must be validated against the XSD:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema targetNamespace="http://www.wbf.org/xml/b2mml-v02"
xmlns="http://www.wbf.org/xml/b2mml-v02"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xsd:include
schemaLocation="B2MML-V02-ProductionPerformance_Globe-V01.xsd"/>
<xsd:annotation>
<xsd:documentation>
</xsd:documentation>
</xsd:annotation>
<!-- Global Elements -->
<xsd:element name="ProductionSchedule" type="ProductionScheduleType"/>
<xsd:element name="ProductionRequest" type="ProductionRequestType"/>
<!-- Simple & Complex Types -->
<xsd:complexType name="ProductionScheduleType">
<xsd:sequence>
<xsd:element name="Location" type="LocationType" minOccurs="0"/>
<xsd:element name="PublishedDate" type="PublishedDateType"
minOccurs="0"/>
<xsd:element name="ProductionRequest"
type="ProductionRequestType" minOccurs="0" maxOccurs="unbounded"/>
<xsd:element name="Any" type="AnyType" minOccurs="0"
maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="ProductionRequestType">
<xsd:sequence>
<xsd:element name="ID" type="IDType" minOccurs="0"/>
<xsd:element name="SegmentRequirement"
type="SegmentRequirementType" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="SegmentRequirementType">
<xsd:sequence>
<xsd:element name="ID" type="IDType" minOccurs="0"/>
<xsd:element name="globe_SegmentState" type="xsd:string"
minOccurs="0" />
</xsd:sequence>
</xsd:complexType>
</xsd:schema>


The C# code I'm currently using (which isn't validating) is as follows:

public class XsdValidation
{
/// <summary>
/// Validation Error Count
/// </summary>
static int errorCount = 0;

/// <summary>
/// Validation Error Message
/// </summary>
static string errorMessage = "";

/// <summary>
/// Validate the XML string against the XSD file
/// </summary>
/// <param name="xsdPath"></param>
/// <param name="xmlDoc"></param>
public void Validate(string xsdPath, string xmlDoc)
{
// only validate if a file has been specified
if ( xsdPath == "" )
{
return;
}

// if the file doesn't exist, this is a problem we need to
report.
// This will stop the message being processed, preventing any
messages
// that may be invalid from passing through BIF
if ( !File.Exists ( xsdPath ) )
{
throw new Exception("The Schema Validation file
'"+xsdPath+"' does not exist.");
}

// Declare local objects
XmlTextReader tr = null;
XmlSchemaCollection xsc = null;
XmlValidatingReader vr = null;

// Text reader object
tr = new XmlTextReader(xsdPath);
xsc = new XmlSchemaCollection();
xsc.Add(null, tr);


// XML validator object
vr = new XmlValidatingReader( xmlDoc, XmlNodeType.Document,
null);
vr.Schemas.Add(xsc);


// Add validation event handler
vr.ValidationType = ValidationType.Schema;
vr.ValidationEventHandler +=
new ValidationEventHandler(ValidationHandler);


// Validate XML data, if there's a problem the
// event is fired and errorMessage is constructed
while(vr.Read());

vr.Close();

// Raise exception, if XML validation fails
if (errorCount > 0)
{
string reportedErrorMessage = errorMessage;

errorCount = 0;
errorMessage = "";

throw new Exception("XML Message Validation Failed:\r\n" +
reportedErrorMessage);
}
}


/// <summary>
/// Event handler catching the problems with this XML
/// </summary>
/// <param name="sender"></param>
/// <param name="args"></param>
static void ValidationHandler(object sender,
ValidationEventArgs args)
{
errorMessage += args.Message + "\r\n";
errorCount ++;
}
}
 
M

Martin Honnen

Dan said:
There's an XML message I have, that has no namespace information.
Then there is a XSD schema that is must validate against, but this has a
targetNamespace and xmlns of "http://www.wbf.org/xml/b2mml-v02".
How do I get this XML to validate against the Schema in C#?
<?xml version="1.0" encoding="UTF-8"?>
<ProductionSchedule>
Which must be validated against the XSD:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema targetNamespace="http://www.wbf.org/xml/b2mml-v02"
xmlns="http://www.wbf.org/xml/b2mml-v02"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xsd:element name="ProductionSchedule" type="ProductionScheduleType"/>

Well the validation should tell you that the XML is not valid according
to the schema, if you want a file that is valid according to the schema
then you need to have
<ProductionSchedule xmlns="http://www.wbf.org/xml/b2mml-v02">
or
<ex:productionSchedule xmlns:ex="http://www.wbf.org/xml/b2mml-v02">
It is as simple as that, if the schema defines the element
ProductionSchedule to be in a certain namespace then any XML instance
having the element in the null namespace or another namespace is not
valid according to the schema.

So you need to either change the schema or the XML instance file if you
want the XML instance to be valid according to the schema.
 

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