473,503 Members | 9,912 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 6898


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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1441
by: AJM | last post by:
I am having a problem getting the schema validation to work. Using any of the samples, i.e. SAXCount DOMCount DOMPrint etc, when trying to run the v=always option and then using the...
9
2919
by: Rob Mayo | last post by:
I have a bunch of XSD files in my assembly as embedded content that are read out via reflection and streams. My app creates the XML on the fly, and I want to validate it using the schema files...
1
1209
by: Julian Hershel | last post by:
Hi. Does the Validate Schema menu option interpret any feature of a .xsd file? I am trying to test a simple extension from a complextype, but when I try to validate my XML file, the validation...
0
467
by: Raj | last post by:
Hi, All Iam trying to do is validate a group of schemas which inturn references an external XSD schema(tried putting both in local webserver/as part of project),Iam getting errors This is what Iam...
1
9086
by: aevans1108 | last post by:
Greetings All If this is the wrong place to post this question, please give me a push in the right direction. Thanks. I know there has to be a simpler way to do this, but this is as simple a...
0
4562
by: SHC | last post by:
Hi all, I have a VC++ .NET 2003 - Windows XP Pro PC. I created a Win32 console application in my VC++ .NET 2003 and copied validateDOM.cpp, books.xml and books.xsd (see the attached files below)...
4
2273
by: bibsoconner | last post by:
Hi, I hope someone can please help me. I'm having a lot of trouble with schema files in .NET. I have produced a very simple example that uses "include" to include other schema files. It all...
10
1964
by: Mike Logan | last post by:
I am using the "contract first" design methodology. Contract First is design the WSDL first then design the server and client. However I must design my XSD/XML Schema before anything. I am...
2
3014
by: Mark | last post by:
Hi... I've been trying the .Validate() method on the XmlDocument to validate some xml against a schema, but one thing I noted was that unless the document explicitly declares the schema as a...
4
9100
by: syed.akhlaq | last post by:
Hi, Does anyone know how can I validate XPath expressions using xsd schema? Thanks
0
7207
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7294
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7361
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
7470
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...
1
5026
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4693
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...
0
1523
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 ...
1
749
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
403
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...

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.