本文介绍了反序列化XML包含无效的数据类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我需要反序列化到对象与此类似的XML文件:

I have an XML file that I need to deserialize into an object similar to this one:

public class TestObject
{
    public string Name { get; set; }
    public int Size { get; set; }

    public TestObject()
    {
        Name = string.Empty;
        Size = 0;
    }
}

我的Deserialize方法是这样的:

My deserialize method looks like this:

private TestObject DeserializeConfiguration(string xmlFileName)
{
    XmlSerializer deserializer = new XmlSerializer(typeof(TestObject));
    TextReader textReader = new StreamReader(xmlFileName);
    TestObject testObj = (TestObject)deserializer.Deserialize(textReader);
    textReader.Close();
    return testObj;
}

这是工作不够好,对我来说,但有时,我得到了可能包含无效数据类型的XML文件(无效的,我指的是相对于对象的属性,它应该映射到的类型)。例如,如果我的XML文件是这样的:

This is working well enough for me but on occasion, I get an XML file that may contain an invalid data type (by "invalid", I mean with respect to the type of the object property that it should map to). For example, if my XML file looks like this:

<?xml version="1.0" encoding="utf-8"?>
<TestObject xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Name>Orion</Name>
  <Size>abc</Size>
</TestObject>

很明显,我不能将ABC在我的对象的整数Size属性。当我尝试反序列化这一点,我看到一个InvalidOperationException,毫不奇怪,设置InnerException是输入字符串的不正确的格式。是否有可能赶上这个错误,使用默认值,我对象的财产,并继续反序列化的XML文件的其余部分?如果没有,谁能告诉我,如果有一个普遍视为最佳实践为反序列化过程中处理无效的数据?

Obviously I can't convert "abc" into the integer Size property of my object. When I attempt to deserialize this, I see an InvalidOperationException and, not surprisingly, the InnerException is "Input string was not in a correct format". Is it possible to catch this error, use a default value for that property of my object and continue deserializing the remainder of the XML file? If not, can anyone tell me if there's a generally regarded "best practice" for handling invalid data during deserialization?

推荐答案

什么,你需要做的是反序列化之前验证输入的XML。基本上你想避免处理不好的XML。验证之后解串器至少可以肯定的是所有传入的XML将deserializable。

What you would need to do is to validate the incoming XML before deserializing. Basically you want to avoid having to process badly-formed XML. After validation the deserializer can at least be sure that all incoming XML will be deserializable.

您可以先创建一个包含在你的情况下有效的XML定义的XML架构,然后验证传入的XML与XSD(XML模式定义)(另见<一href="http://www.$c$cguru.com/csharp/csharp/cs_data/xml/article.php/c6737/Validation-of-XML-with-XSD.htm" rel="nofollow">http://www.$c$cguru.com/csharp/csharp/cs_data/xml/article.php/c6737/Validation-of-XML-with-XSD.htm了解更多详情)。

You can create an XML Schema that contains the definition of valid XML in your case, and then validate the incoming XML with the XSD (XML Schema Definition) first (see also http://www.codeguru.com/csharp/csharp/cs_data/xml/article.php/c6737/Validation-of-XML-with-XSD.htm for more details).

祝你好运!

这篇关于反序列化XML包含无效的数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 05:29