By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,209 Members | 1,706 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,209 IT Pros & Developers. It's quick & easy.

validation of an xml file against multiple defined schema

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.