XmlSerializer的枚举反序列化失败的(不存在)空白 [英] XmlSerializer enumeration deserialization failing on (non existent) whitespace

查看:402
本文介绍了XmlSerializer的枚举反序列化失败的(不存在)空白的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我反序列化,其中包含一个枚举'JourneyPatternLinkDirectionEnumeration,它是作为一个节点'方向'a值的对象。

I have an object I'm deserializing which contains an enum 'JourneyPatternLinkDirectionEnumeration', it's used as a value for a node 'Direction'.

在方向与指定的值,或者没有指定而且它在XML中表示为

When 'Direction' is specified with a value, or not specified and it's represented in xml as

<Direction />



一切工作正常。但是,如果它在XML作为

Everything works fine. However, if it's in the xml as

<Direction></Direction>



我收到以下错误:

I get the following error:

实例验证错误:''不是
JourneyPatternLinkDirectionEnumeration
A有效的价值。

"Instance validation error: '' is not a valid value for JourneyPatternLinkDirectionEnumeration."

我的代码如下:

var xmlTextReader = new XmlTextReader(xmlDocUri);
xmlTextReader.WhitespaceHandling = WhitespaceHandling.None;
xmlTextReader.Normalization = false;

var serializer = new XmlSerializer(typeof(T),  typeof(T).Assembly.GetTypes());

ouput = (T)serializer.Deserialize(xmlTextReader);

有什么想法?有没有更好的方式来做到这一点。

Any thoughts? Is there a better way to do this.

(对不起,我不能发布完整的代码,该XML文档是65000行TransXchange DOC)

(Sorry I can't post the full code, the xml doc is a 65000-line TransXchange doc)

推荐答案

有几个地方的的的(自闭)元素获得视作显著不同的一个元素。有一个空的文本内容

There are a few places where an empty (self-closing) element gets treated as significantly different to an element that has an empty text content.

假设你不控制源头,我不知道如果在这种情况下,你应该务实,将其更改为一个字符串成员:

Assuming you don't control the source, I wonder if in this case you should be pragmatic and change it to a string member:

public string Direction {
    get { return enumField.ToString(); }
    set { enumField = (EnumType)Enum.Parse(value,typeof(EnumType));}
}

这篇关于XmlSerializer的枚举反序列化失败的(不存在)空白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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