XML针对XSD架构进行验证 [英] XML Validate against XSD Schema

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

问题描述

嘿大师

我正在尝试用C#编写一个validate函数.
这是试图做的事情.
正在从Webservices获取XML,并已针对XSD模式文件进行了验证.
但是我怎么会错过代码中的某些内容,因此无法验证.
请给我一点光.

Hey guru''s

Am trying to write a validate function in C#.
Here is what am trying to do.
Am getting XML from Webservices and am validate against XSD schema file.
But some how i missed something in code,so its not validate.
Please throw me some light.

public static bool validateXml(string strXml, string strXsdFilePath)
        {
            bool validity = false;

            try
            {
                //step 1: Read the XSD file and create xml schema object from it
                StreamReader xsdReader = new StreamReader(strXsdFilePath);
                XmlSchema schema = new XmlSchema();
                schema = XmlSchema.Read(xsdReader, new ValidationEventHandler(XSDValidationEventHandler));

                //step 2: Instantiate xml reader settings so that xml file
                //is actually read according to the configured settings
                XmlReaderSettings readerSettings = new XmlReaderSettings();
                readerSettings.ValidationType = ValidationType.Schema;
                readerSettings.Schemas.Add(schema);
                readerSettings.ValidationEventHandler += new ValidationEventHandler(XMLValidationEventHandler);

                //step 3: Read and validate xml file
                StringReader xmlReader = new StringReader(strXml);
                XmlReader objXmlReader = XmlReader.Create(xmlReader, readerSettings);
                while (objXmlReader.Read())
                { }

                //if at this point, then xml has been validated against the xsd file
                validity = true;
                //log the success
                log.Info("Successfule xml file validation");
            }
            catch (Exception error)
            {
                validity = false;
                // XML Validation failed
                log.Error( "XML validation failed:- ERROR MESSAGE: " + error.Message);
                
            }

            return validity;
        }



我的XSD架构文件



My XSD Schema File

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="CRS_Test.dtd" xmlns:wmh="http://www.wmhelp.com/2003/eGenerator" elementformdefault="qualified" targetnamespace="CRS_Test.dtd">
  <xs:element name="TEST">
    <xs:complextype />
  </xs:element>
</xs:schema>


my XML File
<test> </test>



在此先感谢.



Thanks in Advance.

推荐答案

这里的明显问题是测试与TEST不匹配.在您的模式中,您已经定义了TEST的元素,但是您的XML文件显示为test. XML区分大小写,因此两者不匹配.
The obvious issue here is that test does not match TEST. In your schema, you''ve defined an element of TEST, but your XML file says test. XML is case sensitive, so the two do not match.


首先从您的XSD创建代理类
为此VS命令提示符输入follwoing comamd
xsd.exe a.xsd/c

其中a.xsd是您的xsd文件的路径,/c代表代理类语言C#.

现在,您可以映射并验证XML文件Againts代理类.
First create the proxy class from your XSD
for this opern VS command prompt and type follwoing comamd
xsd.exe a.xsd /c

where a.xsd is path for your xsd file and /c stands for proxy class language C#.

Now you can maap and validate your XML file againts proxy class.


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

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