473,289 Members | 2,040 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,289 software developers and data experts.

Xml Schema validations and namespaces...


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 ++;
}
}
Nov 12 '05 #1
1 2299


Dan Bass wrote:
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.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Nov 12 '05 #2

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

Similar topics

1
by: Naresh Agarwal | last post by:
Hi I'm using SAX Parser of Xerces Java v2.4.0 for XML Parsing. I want to perform schema validations on the xml. The problem is that root element of XML document does not have...
5
by: Zombie | last post by:
Hi, Can I have 2 namespaces in the same XML schema? In the schema, I wish to declare elements such that some of them belong to one namespace and others belong to a second namespace. Is this...
4
by: Ian | last post by:
I would like to set a path to a schema where both the xml file and the schema are on my local hard drive (e.g. c:\XML\auto.xml and c:\XML\auto.xsd) Thank you, Ian
2
by: Stanimir Stamenkov | last post by:
I'm trying to find out if it is permissible to include a schema document with absent target namespace to a schema with specified target namespace, and if it is, what are the rules to resolve the...
1
by: Ryan | last post by:
I have a very complex XDR schema that uses namespaces: xmlns="urn:schemas-microsoft-com:xml-data" xmlns:b="urn:schemas-microsoft-com:BizTalkServer" xmlns:d="urn:schemas-microsoft-com:datatypes"...
2
by: Rajesh Jain | last post by:
I Have 2 separate schemas. --------------Schema 1 is defined as below----------- <xs:schema targetNamespace="http://Schemas/1" xmlns="http://Schemas/1" xmlns:xs="http://www.w3.org/2001/XMLSchema"...
6
by: Martin | last post by:
Hi, I have a xml file like the one below <?xml version="1.0" encoding="utf-8"?><e1 xmlns:e1="http://tempuri.org/Source1.xsd" e1:att1="1" e1:att2="2" e1:rest="345"/> If I try to create a...
6
by: LesleyW | last post by:
Hi Apologies if this is a really dumb question, but being new to XML and Schemas, I wonder if giving the namespace for eg xsd or xsi as a website address means that the user has to be online...
1
by: Dan Bass | last post by:
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...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...

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.