简体   繁体   English

解析XmlDocument时处理错误

[英]Handling Errors when Parsing XmlDocument

I'm still getting comfortable working with XML files. 我仍然对使用XML文件感到满意。 I've been looking at a number of examples online and have been struck by the lack of error handling. 我一直在网上查看许多示例,但由于缺乏错误处理而感到震惊。

The most common error is something like el.Attributes["name"].Value . 最常见的错误是类似el.Attributes["name"].Value Since XML is human-editable, it's possible that the attribute is missing. 由于XML是人类可编辑的,因此可能缺少该属性。 And trying to reference the Value property on null will raise an exception. 尝试在null上引用Value属性将引发异常。 Other issues would be related to data not being in the expected format. 其他问题将与数据的格式不正确有关。

So I started writing some helper extension methods along the lines of the following: 因此,我开始按照以下方式编写一些辅助程序扩展方法:

public static class XmlHelpers
{
    public static string GetValue(this XmlAttribute attr, string defaultValue = "")
    {
        if (attr != null)
            return attr.Value;
        return defaultValue;
    }

    public static bool GetValueBool(this XmlAttribute attr, bool defaultValue = false)
    {
        bool value;
        if (bool.TryParse(attr.GetValue(), out value))
            return value;
        return defaultValue;
    }
}

I know this will work. 我知道这行得通。 But am I missing anything? 但是我想念什么吗? Does XmlDocument already provide functionality that makes this type of stuff unnecessary? XmlDocument是否已经提供了使这种类型的东西不必要的功能? I'm just wondering how others are dealing with this. 我只是想知道其他人如何处理这个问题。

I realize that many XML files are never edited by humans. 我认识到许多XML文件从来都不是人类编辑的。 And, for that reason, many people may just write code that assumes there will be no errors. 并且,由于这个原因,许多人可能只是编写假定没有错误的代码。 If there is an error, then there's an exception. 如果有错误,那就有例外。 I can understand that. 我能理解 But I was hoping to give my app a little more flexibility if the files are edited by humans, and something isn't formatted quite right. 但是,如果文件是由人编辑的,并且格式不正确,我希望给我的应用程序更多的灵活性。

Depending on what errors you're interested in accommodating (and what the XML you're parsing looks like), the XmlSerializer class might be of use: 根据您想要适应的错误(以及您要解析的XML的样子),可能会使用XmlSerializer类:

void Main()
{
    var xmlSerializer = new XmlSerializer(typeof(Foo));
    var foo1 = (Foo)xmlSerializer.Deserialize(new StringReader(@"<Foo a=""11""></Foo>"));
    Console.WriteLine(foo1.A); // 11

    var foo2 = (Foo)xmlSerializer.Deserialize(new StringReader(@"<Foo></Foo>"));
    Console.WriteLine(foo2.A); // 10 (fell back to the default)

    // throws format exception
    var foo3 = (Foo)xmlSerializer.Deserialize(new StringReader(@"<Foo a=""x""></Foo>"));
}

// Define other methods and classes here
[XmlRoot("Foo")]
public class Foo {
    public Foo() { this.A = 10; }

    [XmlAttribute("a")]
    public int A { get; set; }
}

Handling parsing errors is obviously more difficult. 处理解析错误显然更加困难。 One way could be to use XmlSerializer as described above but use string for all types (perhaps with wrapper properties that incorporate the handling of bad formatting). 一种方法是如上所述使用XmlSerializer,但对所有类型都使用字符串(也许具有包装器属性,该属性包含对不良格式的处理)。 To get more type-safety, you could define custom types that implement IXmlSerializable "safely" and which define implicit conversions to a System type. 为了获得更多的类型安全性,可以定义“安全”实现IXmlSerializable的自定义类型,并定义对系统类型的隐式转换。

You're not really talking about "errors" here, you're talking about things that can legitimately happen. 您并不是在这里真正地谈论“错误”,而是在谈论可能合法发生的事情。 XML is a very flexible format. XML是一种非常灵活的格式。 One of the problems is that conventional programming languages are far less flexible. 问题之一是常规编程语言的灵活性远远不足。

The kind of flexibility you are looking for is built into XML-oriented languages like XPath, XQuery, and XSLT. 您正在寻找的灵活性内置于面向XML的语言中,例如XPath,XQuery和XSLT。 Low-level programming against DOM interfaces is really hard work compared with XPath, for the reasons you are discovering. 由于您发现的原因,与XPath相比,针对DOM接口的低级编程确实非常艰苦。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM