473,626 Members | 3,459 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"?>
<ProductionSche dule>
<Location>
<EquipmentID>09 45</EquipmentID>
<EquipmentEleme ntLevel>Site</EquipmentElemen tLevel>
</Location>
<PublishedDate> 2004-12-08T13:20:27.804 </PublishedDate>
<ProductionRequ est>
<ID>00001000237 7</ID>
<SegmentRequire ment>
<ID>10000000000 0002689</ID>
<globe_SegmentS tate>Finished</globe_SegmentSt ate>
</SegmentRequirem ent>
</ProductionReque st>
</ProductionSched ule>
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" elementFormDefa ult="qualified"
attributeFormDe fault="unqualif ied">
<xsd:include
schemaLocation= "B2MML-V02-ProductionPerfo rmance_Globe-V01.xsd"/>
<xsd:annotation >
<xsd:documentat ion>
</xsd:documentati on>
</xsd:annotation>
<!-- Global Elements -->
<xsd:element name="Productio nSchedule" type="Productio nScheduleType"/>
<xsd:element name="Productio nRequest" type="Productio nRequestType"/>
<!-- Simple & Complex Types -->
<xsd:complexTyp e name="Productio nScheduleType">
<xsd:sequence >
<xsd:element name="Location" type="LocationT ype" minOccurs="0"/>
<xsd:element name="Published Date" type="Published DateType"
minOccurs="0"/>
<xsd:element name="Productio nRequest"
type="Productio nRequestType" minOccurs="0" maxOccurs="unbo unded"/>
<xsd:element name="Any" type="AnyType" minOccurs="0"
maxOccurs="unbo unded"/>
</xsd:sequence>
</xsd:complexType >
<xsd:complexTyp e name="Productio nRequestType">
<xsd:sequence >
<xsd:element name="ID" type="IDType" minOccurs="0"/>
<xsd:element name="SegmentRe quirement"
type="SegmentRe quirementType" minOccurs="0" maxOccurs="unbo unded"/>
</xsd:sequence>
</xsd:complexType >
<xsd:complexTyp e name="SegmentRe quirementType">
<xsd:sequence >
<xsd:element name="ID" type="IDType" minOccurs="0"/>
<xsd:element name="globe_Seg mentState" type="xsd:strin g"
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;
XmlSchemaCollec tion xsc = null;
XmlValidatingRe ader vr = null;

// Text reader object
tr = new XmlTextReader(x sdPath);
xsc = new XmlSchemaCollec tion();
xsc.Add(null, tr);
// XML validator object
vr = new XmlValidatingRe ader( xmlDoc, XmlNodeType.Doc ument,
null);
vr.Schemas.Add( xsc);
// Add validation event handler
vr.ValidationTy pe = ValidationType. Schema;
vr.ValidationEv entHandler +=
new ValidationEvent Handler(Validat ionHandler);
// 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 reportedErrorMe ssage = errorMessage;

errorCount = 0;
errorMessage = "";

throw new Exception("XML Message Validation Failed:\r\n" +
reportedErrorMe ssage);
}
}
/// <summary>
/// Event handler catching the problems with this XML
/// </summary>
/// <param name="sender"></param>
/// <param name="args"></param>
static void ValidationHandl er(object sender,
ValidationEvent Args args)
{
errorMessage += args.Message + "\r\n";
errorCount ++;
}
}
Nov 16 '05 #1
1 3071


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"?>
<ProductionSche dule> 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" elementFormDefa ult="qualified"
attributeFormDe fault="unqualif ied"> <xsd:element name="Productio nSchedule" type="Productio nScheduleType"/>


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
<ProductionSche dule xmlns="http://www.wbf.org/xml/b2mml-v02">
or
<ex:ProductionS chedule xmlns:ex="http://www.wbf.org/xml/b2mml-v02">
It is as simple as that, if the schema defines the element
ProductionSched ule 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
6372
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 "xsi:schemaLocation" attribute, which is used to specify the .xsd file. Is there any other way for specifying the name of .xsd file?
5
2237
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 possible? Note that both the namespaces should be in the same schema and same xsd file. Could somebody provide a small snippet on how to do this? Thanks for your time.
4
3541
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
2486
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 target namespace of the components from the included schema document. I'm confused because of the rules I read in the XML Schema spec <http://www.w3.org/TR/xmlschema-1/#element-element>: > If the <element> element information item has <schema>...
1
1829
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" Do I need to build a custom validator in order to validate XML documents against this schema? How does the XML parser know how to validate against the extra namespaces?
2
1527
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" elementFormDefault="qualified" attributeFormDefault="unqualified"> <xs:element name="Loan"> <xs:complexType> <xs:sequence> <xs:element name="Borrower" maxOccurs="unbounded"> <xs:complexType> <xs:attribute name="BorrID" use="required">
1
2321
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 "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...
6
2578
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 schema for it with Visual Studio, I get the error "Failed to create a schema for this data file because:
2
6924
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 follows: <silcn:silcn xmlns:silcn='http://silcn.org/200309' xmlns='http://xmlprobe.com/200312'> it contains an element <report> off the root and also a separate <Silcn:report> again off the root.
6
4429
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 when they run the app? If so, what should I do if I can't guarantee that they will be? Can I bring all these files into the app? Is that desirable? I'm about to write an app that takes an XML file as input, using Visual Studio 2005 Express and SQL...
0
8202
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8366
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7199
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5575
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4093
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4202
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2628
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 we have to send another system
1
1812
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1512
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.