473,395 Members | 1,706 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,395 software developers and data experts.

validation of an xml file against multiple defined schema

does anyone know how to validate an xml file against multiple defined
schema?
can you show me some examples?

i'm making here an xml reader..

thank you

Sep 4 '06 #1
5 4924
Paul,

Are you trying to validate different elements of the document against
different schemas, or are you trying to see if one document (in its
entirety) conforms to multiple schemas?

If you are doing the former, then I would create an XmlReader (through
the static Create method) and add the schemas to the XmlSchemaSet instance
you pass to the XmlReaderSettings property. In doing this, the elements,
which conform to different schemas, will be validated.

However, if you are doing the latter, and have say, three schema files
(which have definitions for all the elements in your XML instance) and need
to see if the instance conforms to all three files, then you will have to
create an XmlReader three times, and check to see if the instance conforms
to the schema properly.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"paul_zaoldyeck" <ni*****@yahoo.comwrote in message
news:11**********************@m79g2000cwm.googlegr oups.com...
does anyone know how to validate an xml file against multiple defined
schema?
can you show me some examples?

i'm making here an xml reader..

thank you

Sep 4 '06 #2
oh, thanks for the help.
my problem was on the second situation, one document conforms to
multiple schemas.

i have solved it already through your advice.

but i have another problem now.

my module, which is a utility XMLReader, has to send every request that
the others want.
they will only have to send a method.

for instance:

the one module will send this method

getElement("element","id") -method sent

<xml>
<element>
<id>value</id->accessed value
</element>
</xml>

or

getElement("element","method","id") ->method sent...

<xml>
<element>
<method>
<id>value</id-accessed value
</method>
</element>
</xml>

so, this is how they will request for the value of the xml...a method
will be sent...and i will give what they want...

how will i do this?what' s the best way for this...???

Nicholas Paldino [.NET/C# MVP] wrote:
Paul,

Are you trying to validate different elements of the document against
different schemas, or are you trying to see if one document (in its
entirety) conforms to multiple schemas?

If you are doing the former, then I would create an XmlReader (through
the static Create method) and add the schemas to the XmlSchemaSet instance
you pass to the XmlReaderSettings property. In doing this, the elements,
which conform to different schemas, will be validated.

However, if you are doing the latter, and have say, three schema files
(which have definitions for all the elements in your XML instance) and need
to see if the instance conforms to all three files, then you will have to
create an XmlReader three times, and check to see if the instance conforms
to the schema properly.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"paul_zaoldyeck" <ni*****@yahoo.comwrote in message
news:11**********************@m79g2000cwm.googlegr oups.com...
does anyone know how to validate an xml file against multiple defined
schema?
can you show me some examples?

i'm making here an xml reader..

thank you
Sep 5 '06 #3
Paul,

The easiest way to do this would be to create an XPath expression from
the parameters passed in, something like this:

public XmlNodeList GetElements(params string[] path)
{
// The XPath expression.
StringBuilder xPathBuilder = new StringBuilder();

// Cycle through the elements in path to create the XPath.
foreach (string pathPart in path)
{
// Append to the path.
xPathBuilder.Append(path);
xPathBuilder.Append('\');
}

// Remove the last '\' character.
xPathBuilder.Length = xPathBuilder.Length - 1;

// Now select the element, assume you have a class-level variable named
"document".
return document.SelectNodes(xPathBuilder.ToString());
}

Of course, you could just have a method that takes an XPath expression
as well.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"paul_zaoldyeck" <ni*****@yahoo.comwrote in message
news:11**********************@p79g2000cwp.googlegr oups.com...
oh, thanks for the help.
my problem was on the second situation, one document conforms to
multiple schemas.

i have solved it already through your advice.

but i have another problem now.

my module, which is a utility XMLReader, has to send every request that
the others want.
they will only have to send a method.

for instance:

the one module will send this method

getElement("element","id") -method sent

<xml>
<element>
<id>value</id->accessed value
</element>
</xml>

or

getElement("element","method","id") ->method sent...

<xml>
<element>
<method>
<id>value</id-accessed value
</method>
</element>
</xml>

so, this is how they will request for the value of the xml...a method
will be sent...and i will give what they want...

how will i do this?what' s the best way for this...???

Nicholas Paldino [.NET/C# MVP] wrote:
>Paul,

Are you trying to validate different elements of the document against
different schemas, or are you trying to see if one document (in its
entirety) conforms to multiple schemas?

If you are doing the former, then I would create an XmlReader
(through
the static Create method) and add the schemas to the XmlSchemaSet
instance
you pass to the XmlReaderSettings property. In doing this, the elements,
which conform to different schemas, will be validated.

However, if you are doing the latter, and have say, three schema
files
(which have definitions for all the elements in your XML instance) and
need
to see if the instance conforms to all three files, then you will have to
create an XmlReader three times, and check to see if the instance
conforms
to the schema properly.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"paul_zaoldyeck" <ni*****@yahoo.comwrote in message
news:11**********************@m79g2000cwm.googleg roups.com...
does anyone know how to validate an xml file against multiple defined
schema?
can you show me some examples?

i'm making here an xml reader..

thank you

Sep 5 '06 #4
ok, i'll try at one...thanks again...i'll just update you...
Nicholas Paldino [.NET/C# MVP] wrote:
Paul,

The easiest way to do this would be to create an XPath expression from
the parameters passed in, something like this:

public XmlNodeList GetElements(params string[] path)
{
// The XPath expression.
StringBuilder xPathBuilder = new StringBuilder();

// Cycle through the elements in path to create the XPath.
foreach (string pathPart in path)
{
// Append to the path.
xPathBuilder.Append(path);
xPathBuilder.Append('\');
}

// Remove the last '\' character.
xPathBuilder.Length = xPathBuilder.Length - 1;

// Now select the element, assume you have a class-level variable named
"document".
return document.SelectNodes(xPathBuilder.ToString());
}

Of course, you could just have a method that takes an XPath expression
as well.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"paul_zaoldyeck" <ni*****@yahoo.comwrote in message
news:11**********************@p79g2000cwp.googlegr oups.com...
oh, thanks for the help.
my problem was on the second situation, one document conforms to
multiple schemas.

i have solved it already through your advice.

but i have another problem now.

my module, which is a utility XMLReader, has to send every request that
the others want.
they will only have to send a method.

for instance:

the one module will send this method

getElement("element","id") -method sent

<xml>
<element>
<id>value</id->accessed value
</element>
</xml>

or

getElement("element","method","id") ->method sent...

<xml>
<element>
<method>
<id>value</id-accessed value
</method>
</element>
</xml>

so, this is how they will request for the value of the xml...a method
will be sent...and i will give what they want...

how will i do this?what' s the best way for this...???

Nicholas Paldino [.NET/C# MVP] wrote:
Paul,

Are you trying to validate different elements of the document against
different schemas, or are you trying to see if one document (in its
entirety) conforms to multiple schemas?

If you are doing the former, then I would create an XmlReader
(through
the static Create method) and add the schemas to the XmlSchemaSet
instance
you pass to the XmlReaderSettings property. In doing this, the elements,
which conform to different schemas, will be validated.

However, if you are doing the latter, and have say, three schema
files
(which have definitions for all the elements in your XML instance) and
need
to see if the instance conforms to all three files, then you will have to
create an XmlReader three times, and check to see if the instance
conforms
to the schema properly.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"paul_zaoldyeck" <ni*****@yahoo.comwrote in message
news:11**********************@m79g2000cwm.googlegr oups.com...
does anyone know how to validate an xml file against multiple defined
schema?
can you show me some examples?

i'm making here an xml reader..

thank you
Sep 6 '06 #5
hi, i have this new problem when it comes to xml validation against
multiple defined schema.
if i change my xsd file, it will still display the content of the xml
file though it's already not the same...

here's my code.please help me.

using System;
using System.Xml;
using System.Xml.Schema;
using System.IO;

namespace ConsoleApplication3
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
ValidationEventHandler eventHandler = new
ValidationEventHandler(Class1.ShowCompileErrors);
XmlSchemaCollection myschemacoll = new XmlSchemaCollection();
XmlValidatingReader vr;
FileStream stream;
try
{
stream = new FileStream("c:\\newXMwL.xml", FileMode.Open);
//Load the XmlValidatingReader.
vr = new XmlValidatingReader(stream, XmlNodeType.Element, null);

//Add the schemas to the XmlSchemaCollection object.
myschemacoll.Add("xsdHeadCount", "c:\\headcount.xsd");
myschemacoll.Add("urn:report-schema", "c:\\ReportSchema2.0.xsd");
myschemacoll.Add("urn:parser-schema", "c:\\ParserSchema2.0.xsd");
vr.Schemas.Add(myschemacoll);
vr.ValidationType = ValidationType.Schema;

//int ctr = 0;

while (vr.Read())
{
//Console.WriteLine("reading...{0}", ctr);
//ctr+=1;

// if (vr.HasAttributes)
// {
// Console.WriteLine("Attributes of <" + vr.Name + ">");
// for (int i = 0; i < vr.AttributeCount; i++)
// {
// Console.WriteLine(" {0}", vr[i]);
// }
// // Move the reader back to the element node.
// vr.MoveToElement();
// }

if (vr.IsStartElement())
{
if (vr.IsEmptyElement)
Console.WriteLine("<{0}/>", vr.Name);
else
{
Console.Write("<{0}", vr.Name);
vr.Read(); // Read the start tag.
if (vr.IsStartElement()) // Handle nested elements.
Console.Write("\r\n<{0}>", vr.Name);
Console.WriteLine(vr.ReadString()); //Read the text content of
the element.
}
}

}
Console.WriteLine("Validation completed");
}
//This code catches any XML exceptions.
catch (XmlException XmlExp)
{
Console.WriteLine("Error on your XML File");
Console.WriteLine(XmlExp.Message);
}
//This code catches any XML schema exceptions.
catch (XmlSchemaException XmlSchemaExp)
{
Console.WriteLine("Error on your XSD File");
Console.WriteLine(XmlSchemaExp.Message);
}
//This code catches any standard exceptions.
catch (Exception GeneralExp)
{
Console.WriteLine(GeneralExp.Message);
}
finally
{
//Clean up.
Console.Read();
vr = null;
myschemacoll = null;
stream = null;
}

}

public static void ShowCompileErrors(object sender,
ValidationEventArgs args)
{
Console.WriteLine("Validation Error: {0}", args.Message);
}
}
}
Nicholas Paldino [.NET/C# MVP] wrote:
Paul,

The easiest way to do this would be to create an XPath expression from
the parameters passed in, something like this:

public XmlNodeList GetElements(params string[] path)
{
// The XPath expression.
StringBuilder xPathBuilder = new StringBuilder();

// Cycle through the elements in path to create the XPath.
foreach (string pathPart in path)
{
// Append to the path.
xPathBuilder.Append(path);
xPathBuilder.Append('\');
}

// Remove the last '\' character.
xPathBuilder.Length = xPathBuilder.Length - 1;

// Now select the element, assume you have a class-level variable named
"document".
return document.SelectNodes(xPathBuilder.ToString());
}

Of course, you could just have a method that takes an XPath expression
as well.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"paul_zaoldyeck" <ni*****@yahoo.comwrote in message
news:11**********************@p79g2000cwp.googlegr oups.com...
oh, thanks for the help.
my problem was on the second situation, one document conforms to
multiple schemas.

i have solved it already through your advice.

but i have another problem now.

my module, which is a utility XMLReader, has to send every request that
the others want.
they will only have to send a method.

for instance:

the one module will send this method

getElement("element","id") -method sent

<xml>
<element>
<id>value</id->accessed value
</element>
</xml>

or

getElement("element","method","id") ->method sent...

<xml>
<element>
<method>
<id>value</id-accessed value
</method>
</element>
</xml>

so, this is how they will request for the value of the xml...a method
will be sent...and i will give what they want...

how will i do this?what' s the best way for this...???

Nicholas Paldino [.NET/C# MVP] wrote:
Paul,

Are you trying to validate different elements of the document against
different schemas, or are you trying to see if one document (in its
entirety) conforms to multiple schemas?

If you are doing the former, then I would create an XmlReader
(through
the static Create method) and add the schemas to the XmlSchemaSet
instance
you pass to the XmlReaderSettings property. In doing this, the elements,
which conform to different schemas, will be validated.

However, if you are doing the latter, and have say, three schema
files
(which have definitions for all the elements in your XML instance) and
need
to see if the instance conforms to all three files, then you will have to
create an XmlReader three times, and check to see if the instance
conforms
to the schema properly.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"paul_zaoldyeck" <ni*****@yahoo.comwrote in message
news:11**********************@m79g2000cwm.googlegr oups.com...
does anyone know how to validate an xml file against multiple defined
schema?
can you show me some examples?

i'm making here an xml reader..

thank you
Sep 7 '06 #6

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

Similar topics

1
by: Zandy Marantal | last post by:
Hello everyone, I'm having trouble using Xerces2(2.4, 2.5) when validating against an XML schema if a general entity reference is defined within the XML file. The error I'm getting is this:...
2
by: Olaf Meyer | last post by:
Apprentently xerces 2.6.0 (Java) does not validate against contraints specified in the schema (e.g. constraints specified via unique element). The validation works with the XML editor I'm using...
5
by: Harald Haspl | last post by:
Hello, I've appended a simple XML file and it's appropriate schema below. I want to validate the XML file against the schema with xerces-c. This example contains a section where colours are...
2
by: Sudip Chakraborty | last post by:
Is there a way to see constraint validation errors while loading xml into a DataSet ? I'm interested in the line number in the xml file which is causing the error. I've enclosed the relevant stack...
4
by: Wayne Wengert | last post by:
I have an XML document and a corresponding xsd document I built. I want to validate the xml document against that Schema but when I use the MSDN validator it has no place to select the xsd...
0
by: Matt | last post by:
I have a problem when I select node elements from an xml file and validata each node againts the schema. I use XmlValidatingReader and it complains about elements not being declared. I have...
7
by: christian.eickhoff | last post by:
Hi Everyone, I am currently implementing an XercesDOMParser to parse an XML file and to validate this file against its XSD Schema file which are both located on my local HD drive. For this...
6
by: lists | last post by:
Hi all, I am trying to validate an XML file against an XSD schema file within a ..NET C++ program, but the validation doesn't seem to be occuring. My code is listed below. The validation...
0
by: tanish2k | last post by:
hi. I am using c#, visual studio 2003. I need to validate a xml file against schema which itself has 2 more schema imported under it. i have following 2 xsd files : xsd1 --->...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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...

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.