使用C#架构XML解析 [英] XML Parsing using a schema in C#

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

问题描述

我工作的一些code,它在运行时加载的XML文件。在分钟,我们使用的XmlDocument类来读取XML文件,并缠绕的SelectSingleNode语句的try-catch(这是在起飞的机会,一个节点为空,或者是不存在的,因为我们正在分析用户完成创建XML文件)。

I'm working on some code that loads an xml file at run time. At the minute, we're using the XmlDocument type to read the xml file and wrapping a try-catch around SelectSingleNode statement (this is done on the off chance that a node is null, or isn't there as we're parsing user created xml files).

请注意:我知道的XmlDocument已取代的XDocument。 (根据但由于我们正在与.NET版本3这个MSDN文档的XDocument是不是在.NET 3)可用,我们必须坚持的XmlDocument现在。我们使用.NET 3,适用于各种原因(其中有些是规范相关)。

Please note: I realise that XmlDocument has been replaced by XDocument. However since we're working with .NET version 3 (according to this MSDN document XDocument isn't available in .NET 3), we're having to stick with XmlDocument for now. We're using .NET 3 for a variety of reasons (some of which are spec related).

下面是一个例子我们在做什么在一分钟:

Here's an example of what we're doing at the minute:

private void LoadUserXMLFile ()
{
    XmlDocument xDoc = new XmlDocument();
    XmlTextReader reader = new XmlTextReader(fileName);
    reader.Read();
    xDoc.Load(reader);

    try { firstElementString = xDoc.SelectSingleNode(<path to node>).InnderText);
    catch { <exception handling here > }
    //more SelectSingleNode statements, each wrapped inside
    //individual try-catch blocks
}

显然,上面是一个例子,我已经简化了catch语句。

Obviously the above is an example, and I've simplified the catch statement.

我已经写了该用户的模式生成的XML文件,这个程序将与,我在想,如果我的XML文档的解析过程中使用的模式(以某种方式),将我还需要包装每个用的SelectSingleNode的try-catch语句?

I've written a schema for the user generated XML files that this app will work with, and I was wondering if I used the schema (in some way) during parsing of an XML document, would I still need to wrap each SelectSingleNode with try-catch statements?

它甚至有可能使用的架构(在某种程度上)分析检查XML文档的格式是否正确,并在所有必需的要素是什么?

Is it even possible to use the schema (in some way) during parsing to check the XML document has the correct format and all of the required elements?

推荐答案

是的,你需要使用验证读取器

Yes you need to use a validating reader

您可以使用这样的事情

XmlTextReader r = new XmlTextReader("C:\\Xml\\MyXmlFile.xml");
v.ValidationType = ValidationType.Schema;

显然,你的XML将参考架构,以便多数民众赞成如何引用,该解决(在XML本身),像这样

Obviously your xml will refer to the schema so thats how the reference to that is resolved (in the xml itself) like so

<Employee EmployeeId="12345566" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="Employee.xsd">
   <PersonName>Krystan</PersonName>
</Employee>

如果你不能改变的XML,你可以使用这样XmlReaderSettings

if you cannot change the xml you can use XmlReaderSettings like this

public void SomeMethod()
{
    XmlReaderSettings xmlsettings = new XmlReaderSettings();
    xmlsettings.Schemas.Add("http://www.company.com/blah", "blah.xsd");
    xmlsettings.ValidationType = ValidationType.Schema;
    xmlsettings.ValidationEventHandler += new ValidationEventHandler(ValidationHandler);

    XmlReader reader= XmlReader.Create("somefile.xml", xmlsettings);

    while (reader.Read()) { }
}

public void ValidationHandler(object sender, ValidationEventArgs args)
    void booksSettingsValidationEventHandler(object sender, ValidationEventArgs e)
{
    if (e.Severity == XmlSeverityType.Warning)
    {
        Console.Write("WARNING: ");
        Console.WriteLine(e.Message);
    }
    else if (e.Severity == XmlSeverityType.Error)
    {
        Console.Write("ERROR: ");
        Console.WriteLine(e.Message);
    }
}

要回答你的问题,如果你已经验证对一个架构,并选择已知那里你可以与每个节点异常HANDELING免除,但显然你应该警惕可能到来,因为文件加载等有关异常的节点

to answer your question if you have validated against a schema and are selecting nodes known to be there you can dispense with the per node exception handeling but obviously you should guard against an exception that may come about because of file load etc.

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

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