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 ++;
}
}
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 ++;
}
}