validation of an xml file against multiple defined schema

P

paul_zaoldyeck

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
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 
P

paul_zaoldyeck

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...???
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]
- (e-mail address removed)

paul_zaoldyeck said:
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
 
N

Nicholas Paldino [.NET/C# MVP]

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]
- (e-mail address removed)

paul_zaoldyeck said:
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...???
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]
- (e-mail address removed)

paul_zaoldyeck said:
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
 
P

paulzaoldyeck

ok, i'll try at one...thanks again...i'll just update you...
Nicholas said:
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]
- (e-mail address removed)

paul_zaoldyeck said:
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...???
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]
- (e-mail address removed)

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
 
P

paul_zaoldyeck

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);
// }
// // 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 said:
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]
- (e-mail address removed)

paul_zaoldyeck said:
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...???
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]
- (e-mail address removed)

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top