使用C#通过XSD架构验证XML [英] XML validate by XSD schema with C#

查看:63
本文介绍了使用C#通过XSD架构验证XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在验证无法访问互联网的服务器上的XML文件和

C#中的validadation不起作用。我验证的模式

的命名空间如下:xmlns:ds =" http://www.w3.org/2000/09/xmldsig#"

xmlns:xenc =" http://www.w3.org/2001/04/xmlenc#" ;.


我在磁盘上本地都有这些模式,但确实如此不行。如果

我尝试使用可上网的电脑,那么它的工作正常。

有人知道该怎么做。我验证的功能看起来像

这个:


private bool ValidXml(字符串XmlFile,字符串XSDLoc,字符串tns)

{

//。 。 。

string targetNamespaceURI = tns; //在这里放置目标命名空间URI



// 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)

返回false;

else

返回true;


}


感谢您的帮助...

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

推荐答案



< a href =mailto:g m ***** @ gmail.com> gm ***** @ gmail.com 写道:


gm*****@gmail.com wrote:
我正在验证服务器上的XML文件没有互联网访问和
C#中的validadation不起作用。


究竟什么不起作用你的情况意味着什么?当你运行你的代码时,你得到任何错误

消息吗?确切地说,对于什么行?

我正在验证的模式
具有如下命名空间:xmlns :ds =" http://www.w3.org/2000/09/xmldsig#"
xmlns:xenc =" http://www.w3.org/2001/04/xmlenc#" ;。


命名空间声明可能意味着XML文档在不同的命名空间中包含

元素,并且使用W3C XML模式语言

这意味着每个命名空间至少需要一个模式模块。

因此,您的模式可能在公共Internet HTTP服务器上有xs:import或xs:include引用

reosurce ?


private bool ValidXml(字符串XmlFile,字符串XSDLoc,字符串tns)
{
//。 。 。
字符串targetNamespaceURI = tns; //在这里放置模式的目标名称空间URI
// 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);
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);




代码失败时XSDLoc的价值究竟是什么?


-


Martin Honnen --- MVP XML
http:/ /JavaScript.FAQTs.com/


很抱歉我很缺乏。


所以也许你的架构有一个xs:import或xs:include在一个公共互联网HTTP服务器上引用一个

reosurce?


是的,我有两个http:/的xs:import /www.w3.org/2000/09/xmldsig#"和
http://www.w3.org/2001/ 04 / xmlenc#

导入如下所示:

< 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" />


上层架构地点是相对的,我已经厌倦了绝对而且

没有用。


所以enc-schema.xsd和xmldsig-core- schema.xsd与

我的XSD架构在同一个文件夹中。


究竟什么不起作用你的情况意味着什么?当你运行你的代码时,你得到任何错误

消息吗?确切地说,对于什么行?


嗯...是的我收到错误消息,但我不能确切地告诉你这是什么,因为我现在不在合适的电脑上。此行中的

错误符合vr.Schemas.Add(targetNamespaceURI,XSDLoc);

XSDLoc = Server.MapPath(" myschema.xsd");


错误消息说它找不到EncryptedData。在xenc-schema.xsd中定义了




我试图用本地位置替换xmlns但没有运气。

是否有一些技巧...... dot net中的XML验证在验证时不会转到

互联网?


感谢您的帮助...

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




gm *****@gmail.com 写道:



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

是的,我有http://www.s3.org/的xs:import 2000/09 / XMLDSIG#"和
http://www.w3.org/2001/ 04 / xmlenc#
导入如下所示:
< 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" />

上层架构位置是相对的,我已经厌倦了绝对而且它没有用。

因此,enc-schema.xsd和xmldsig-core-schema.xsd与我的XSD架构位于同一文件夹中。


如果这些模式在本地可用那么那些xs:import不应该是一个问题我认为解析器应该寻找本地模式。


嗯......是的,我收到了一条错误信息,但我不能确切地告诉你它是什么,因为我不是在合适的电脑上此时此刻。
错误在此行中出现vr.Schemas.Add(targetNamespaceURI,XSDLoc);

XSDLoc = Server.MapPath(" myschema.xsd");
错误消息表明它找不到EncryptedData。它是在xenc-schema.xsd中定义的。
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.



你是否会抛出一个中止.NET代码的.NET异常?或者

您的验证事件处理程序报告验证错误消息?


-


Martin Honnen - - MVP XML
http://JavaScript.FAQTs.com/


这篇关于使用C#通过XSD架构验证XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆