验证对XSD XML在一个单一的方法 [英] Validate XML against XSD in a single method

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

问题描述

我需要实现一个需要验证XML对外部XSD,并返回一个布尔结果表明它是否合式或不是C#方法。

 公共静态布尔IsValidXml(字符串xmlFilePath,串xsdFilePath); 



我知道如何的验证使用回调。我想知道它是否可以在一个单一的方法来进行,而无需使用一个回调。我需要这个纯粹是为了美容目的:我需要验证多达几十个类型的XML文档,所以我想提出的是如下这样简单的事情

 如果(!XmlManager.IsValidXml(
@ProjectTypes\ProjectType17.xml,
@Schemas\Project.xsd))
$ { b $ b抛出新XmlFormatException(
的String.Format(
的XML{0}是无效的。,
xmlFilePath));
}


解决方案

有几个选项我能想到这取决于你是否要用于非特殊事件的异常。



如果你传递一个null作为验证回调委托,大部分建-in验证方法会抛出一个异常,如果是严重形成的XML,所以你可以简单地捕捉异常并返回真正 / 视情况而定。

 公共静态布尔IsValidXml(字符串xmlFilePath,串xsdFilePath)
{
XDOC VAR = XDocument.Load(xmlFilePath);
VAR模式=新的XmlSchemaSet();
schemas.Add(namespaceName,xsdFilePath);


{
xdoc.Validate(架构,NULL);
}
赶上(XmlSchemaValidationException)
{
返回FALSE;
}

返回真;
}



,想到另一种选择推你的<$ C $的极限C>不使用回调标准。相反,通过一个预先定义的回调方法,可以转而通过一个匿名方法,并用它来设置一个真正 / 返回值。

 公共静态布尔IsValidXml(字符串xmlFilePath,串xsdFilePath)
{
变种= XDOC XDocument.Load(xmlFilePath);
VAR模式=新的XmlSchemaSet();
schemas.Add(namespaceName,xsdFilePath);

布尔结果= TRUE;
xdoc.Validate(模式,(发件人,E)=>
{
结果=假;
});

返回结果;
}


I need to implement a C# method that needs to validate an XML against an external XSD and return a Boolean result indicating whether it was well formed or not.

public static bool IsValidXml(string xmlFilePath, string xsdFilePath);

I know how to validate using a callback. I would like to know if it can be done in a single method, without using a callback. I need this purely for cosmetic purposes: I need to validate up to a few dozen types of XML documents so I would like to make is something as simple as below.

if(!XmlManager.IsValidXml(
    @"ProjectTypes\ProjectType17.xml",
    @"Schemas\Project.xsd"))
{
     throw new XmlFormatException(
         string.Format(
             "Xml '{0}' is invalid.", 
             xmlFilePath));
}

解决方案

There are a couple of options I can think of depending on whether or not you want to use exceptions for non-exceptional events.

If you pass a null as the validation callback delegate, most of the built-in validation methods will throw an exception if the XML is badly formed, so you can simply catch the exception and return true/false depending on the situation.

public static bool IsValidXml(string xmlFilePath, string xsdFilePath)
{
    var xdoc = XDocument.Load(xmlFilePath);
    var schemas = new XmlSchemaSet();
    schemas.Add(namespaceName, xsdFilePath);

    try
    {
        xdoc.Validate(schemas, null);
    }
    catch (XmlSchemaValidationException)
    {
        return false;
    }

    return true;
}

The other option that comes to mind pushes the limits of your without using a callback criterion. Instead of passing a pre-defined callback method, you could instead pass an anonymous method and use it to set a true/false return value.

public static bool IsValidXml(string xmlFilePath, string xsdFilePath)
{
    var xdoc = XDocument.Load(xmlFilePath);
    var schemas = new XmlSchemaSet();
    schemas.Add(namespaceName, xsdFilePath);

    Boolean result = true;
    xdoc.Validate(schemas, (sender, e) =>
         {
             result = false;
         });

    return result;
}

这篇关于验证对XSD XML在一个单一的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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