473,387 Members | 1,535 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,387 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 2307


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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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,...

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.