473,499 Members | 1,862 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 16 '05 #1
1 3064


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 16 '05 #2

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

Similar topics

1
6344
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
2219
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
3527
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
2467
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
1816
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
1509
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"...
1
2313
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...
6
2555
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...
2
6894
by: PeterW | last post by:
I have an xml file from which I want to generate an xsd schema and at a later stage a cs class. The xml file has a mix of defined namespaces and also an empty namespace. These are defined as...
6
4416
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...
0
7134
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
7012
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
7180
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,...
0
7392
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4920
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4605
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3105
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1429
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
307
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.