470,632 Members | 2,300 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,632 developers. It's quick & easy.

XML validate by XSD schema with C#

I am validating XML files on a server which has no internet access and
the validadation in C# does not work. Schema with which I am validating
has namespaces like: xmlns:ds="http://www.w3.org/2000/09/xmldsig#"
xmlns:xenc="http://www.w3.org/2001/04/xmlenc#".

I have these schemas all locally on the disk, but it does not work. If
I try on a computer with internet access then it works just fine. Does
someone know what to do. Function with which I validate looks like
this:

private bool ValidXml(string XmlFile, string XSDLoc, string tns)
{
// . . .
string targetNamespaceURI = tns; // put the target namespace URI
of your schema here.
//XmlValidatingReader vr = new XmlValidatingReader( new
XmlTextReader( new StringReader( doc ) ) );
XmlValidatingReader vr = new XmlValidatingReader( new
XmlTextReader(XmlFile) );
vr.XmlResolver = new XmlUrlResolver();
MessageBox.Show(vr.NamespaceURI);
vr.Schemas.Add( targetNamespaceURI, XSDLoc);
vr.ValidationEventHandler += new ValidationEventHandler
(ValidationHandler);
vr.ValidationType = ValidationType.Schema;
_ValidationErrorsCount = 0;
_ValidationErrorsMsgs = "";
while(vr.Read());
vr.Close();
if (_ValidationErrorsCount > 0)
return false;
else
return true;

}

Thanks for any help ...

Mar 31 '06 #1
10 6662


gm*****@gmail.com wrote:
I am validating XML files on a server which has no internet access and
the validadation in C# does not work.
What exactly "does not work" mean in your case? Do you get any error
mesage when you run your code, which exactly, for what line?
Schema with which I am validating
has namespaces like: xmlns:ds="http://www.w3.org/2000/09/xmldsig#"
xmlns:xenc="http://www.w3.org/2001/04/xmlenc#".
Namespaces declarations could mean that the XML document contains
elements in different namespace and with the W3C XML schema language
that means there needs to be at least one schema module per namespace.
So perhaps your schema has an xs:import or xs:include referencing a
reosurce on a public internet HTTP server?

private bool ValidXml(string XmlFile, string XSDLoc, string tns)
{
// . . .
string targetNamespaceURI = tns; // put the target namespace URI
of your schema here.
//XmlValidatingReader vr = new XmlValidatingReader( new
XmlTextReader( new StringReader( doc ) ) );
XmlValidatingReader vr = new XmlValidatingReader( new
XmlTextReader(XmlFile) );
vr.XmlResolver = new XmlUrlResolver();
MessageBox.Show(vr.NamespaceURI);
vr.Schemas.Add( targetNamespaceURI, XSDLoc);


What exactly is the value of XSDLoc when your code fails?

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Mar 31 '06 #2
Sorry that I was so deficient.

So perhaps your schema has an xs:import or xs:include referencing a
reosurce on a public internet HTTP server?

Yes, I have xs:import for both http://www.w3.org/2000/09/xmldsig#" and
http://www.w3.org/2001/04/xmlenc#.
Import looks like this:
<xsd:import namespace="http://www.w3.org/2001/04/xmlenc#"
schemaLocation="xenc-schema.xsd"/>
<xsd:import namespace="http://www.w3.org/2000/09/xmldsig#"
schemaLocation="xmldsig-core-schema.xsd"/>

Upper schema locations are relative, I've tired with absolute and it
didn't work.

So enc-schema.xsd and xmldsig-core-schema.xsd are in the same folder as
my XSD Schema.

What exactly "does not work" mean in your case? Do you get any error
mesage when you run your code, which exactly, for what line?

Huh ... yes I do get an error message, but I can't tell you exactly
what it is, because I am not at the right computer at the moment. The
error accours in this line vr.Schemas.Add( targetNamespaceURI, XSDLoc);
XSDLoc = Server.MapPath("myschema.xsd");

The error message says that it can not find EncryptedData. It is
defined in xenc-schema.xsd.

I have tried to replace xmlns with local locations but no luck.

Is there some trick ... that XML validation in dot net does not go to
internet while validaiting?

Thanks for the help ...

Apr 1 '06 #3


gm*****@gmail.com wrote:

Yes, I have xs:import for both http://www.w3.org/2000/09/xmldsig#" and
http://www.w3.org/2001/04/xmlenc#.
Import looks like this:
<xsd:import namespace="http://www.w3.org/2001/04/xmlenc#"
schemaLocation="xenc-schema.xsd"/>
<xsd:import namespace="http://www.w3.org/2000/09/xmldsig#"
schemaLocation="xmldsig-core-schema.xsd"/>

Upper schema locations are relative, I've tired with absolute and it
didn't work.

So enc-schema.xsd and xmldsig-core-schema.xsd are in the same folder as
my XSD Schema.
If those schemas are available locally then those xs:import should not
be a problem I think as that way the parser should look for a local schema.

Huh ... yes I do get an error message, but I can't tell you exactly
what it is, because I am not at the right computer at the moment. The
error accours in this line vr.Schemas.Add( targetNamespaceURI, XSDLoc);
XSDLoc = Server.MapPath("myschema.xsd");

The error message says that it can not find EncryptedData. It is
defined in xenc-schema.xsd.


Do you get a .NET exception thrown that aborts your .NET code? Or is
that a validation error message your validation event handler reports?

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Apr 2 '06 #4
> >
XSDLoc = Server.MapPath("myschema.xsd");

The error message says that it can not find EncryptedData. It is
defined in xenc-schema.xsd.


Do you get a .NET exception thrown that aborts your .NET code? Or is
that a validation error message your validation event handler reports?


So the exact error message is: The
'http://www.w3.org/2001/04/xmlenc#:EncryptedData' element is not
declared. An error occurred at file:///d:/My
Documents/webservice/myschema.xsd, (46, 6).

I handle the exception - the code:

try{
if (ValidXml(XmlLoc.Text, ShemaLoc.Text, TNS.Text))
MessageBox.Show("XML OK.");
else
MessageBox.Show("Error:\n" + _ValidationErrorsMsgs);
}catch (Exception ex){
MessageBox.Show("Error:\n" + ex.Message);
}

_ValidationErrorsMsgs is variable I fill in ValidationHandler function:

public static void ValidationHandler(object sender, ValidationEventArgs
args)
{
_ValidationErrorsMsgs += "\tSeverity:" + args.Severity + "\t" +
args.Message + "\n";
_ValidationErrorsCount++;
}
The error message I get is in ex.Message.

Any ideas?

Thank you Martin for dealing with my problem.

Apr 3 '06 #5
Is the problem maybe something with the XmlResolver class ... I don't
understand what exactly this class is it about ... but it could resolve
my problem :)))))

Can someone explain what is XmlResolver about?

Apr 5 '06 #6
XmlResolver is used to resolve external XML resources, such as entities,
document type definitions (DTDs), or schemas. It is also used to process
include and import elements found in Extensible StyleSheet Language (XSL)
style sheets or XML Schema definition language (XSD) schemas.

More information about it:
http://msdn2.microsoft.com/en-us/lib...er(VS.80).aspx

--
Stan Kitsis
Program Manager, XML Technologies
Microsoft Corporation

This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm
<gm*****@gmail.com> wrote in message
news:11*********************@t31g2000cwb.googlegro ups.com...
Is the problem maybe something with the XmlResolver class ... I don't
understand what exactly this class is it about ... but it could resolve
my problem :)))))

Can someone explain what is XmlResolver about?

Apr 5 '06 #7
Thanks Stan, but I still don't know how use this XmlResolver in the way
that would solve my problem ...

Now I've tried like this:
Line1: FileStream fs = new FileStream("d:\\My
Documents\\xmldsig-core-schema.xsd", FileMode.Open);
Line 2: XmlSchema dsigSchema = XmlSchema.Read(fs, new
ValidationEventHandler(ValidationHandler));

In the line 2 there is an exception. The error message is:
The underlying connection was closed: The remote name could not be
resolved.

xmldsig-core-schema.xsd is schema from
http://www.w3.org/2000/09/xmldsig# and as you can see it's localy on my
computer.

It comes to this exception just in case that I am offline, when I plug
in my network cable and connect to internet ... the exception
disappers.

What should I do? Please help.

Apr 6 '06 #8
So nobody had such problem ... well I was unable to solve it ... so I
used MSXML 4.0 functions in .NET and that works fine.

Apr 9 '06 #9
I am having same issue as your please see my posting

http://groups.google.com/group/micro...cc947c6ca93c83

I am connected to internet so not sure why import will not get the
relevent schema.

Can you please post your solution.

Apr 29 '06 #10
I've just added reference to msxml4.dll and called functions to
validate xml by xsd.

May 8 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by AJM | last post: by
1 post views Thread by Julian Hershel | last post: by
4 posts views Thread by syed.akhlaq | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.