C# 反序列化 XML
我在使用 XmlSerializer
类将文档反序列化为对象时遇到问题。
为反序列化编写我的函数:
static public TYPE xmlToObject<TYPE>( string xmlDoc ) {
MemoryStream stream = new MemoryStream();
byte[] xmlObject = ASCIIEncoding.ASCII.GetBytes( xmlDoc );
stream.Write( xmlObject, 0, xmlObject.Length );
stream.Position = 0;
TYPE message;
XmlSerializer xmlSerializer = new XmlSerializer( typeof( TYPE ) );
try {
message = (TYPE)xmlSerializer.Deserialize( stream );
} catch ( Exception e ) {
message = default( TYPE );
} finally {
stream.Close();
}
return message;
}
我有类:
public class Test {
public int a;
public int b;
}
并且反序列化:
string text = File.ReadAllText( "blue1.xml" );
Test a = XmlInterpreter.xmlToObject<Test>( text );
好的,当我像这样读取文件时:
<?xml version="1.0" encoding="UTF-8"?>
<Test>
<a>2</a>
<b>5</b>
</Test>
一切正常。但像这样:
<?xml version="1.0" encoding="UTF-8"?>
<Test>
<a>2</a>
<b></b>
</Test>
是错误的,因为
<b></b>
它是空的并且转换为 int 是不可能的。
我该如何解决这个问题?例如,我希望在这种情况下不会声明 b
。
当我的类是:
public class Test {
public enum Pro {
VALUE1,
VALUE2
}
public Pro p1;
}
并且我想要接受 xmlDocument
,其中字段 p1
为空时。
I have problem with deserialize document to object using XmlSerializer
class.
Code my function for deserialize:
static public TYPE xmlToObject<TYPE>( string xmlDoc ) {
MemoryStream stream = new MemoryStream();
byte[] xmlObject = ASCIIEncoding.ASCII.GetBytes( xmlDoc );
stream.Write( xmlObject, 0, xmlObject.Length );
stream.Position = 0;
TYPE message;
XmlSerializer xmlSerializer = new XmlSerializer( typeof( TYPE ) );
try {
message = (TYPE)xmlSerializer.Deserialize( stream );
} catch ( Exception e ) {
message = default( TYPE );
} finally {
stream.Close();
}
return message;
}
And I have class:
public class Test {
public int a;
public int b;
}
And deserialize:
string text = File.ReadAllText( "blue1.xml" );
Test a = XmlInterpreter.xmlToObject<Test>( text );
Ok, when I read file like this:
<?xml version="1.0" encoding="UTF-8"?>
<Test>
<a>2</a>
<b>5</b>
</Test>
everything is OK. But like this:
<?xml version="1.0" encoding="UTF-8"?>
<Test>
<a>2</a>
<b></b>
</Test>
is wrong because
<b></b>
is empty and conversion into int is impossible.
How can I solve this? For example I want in this context that b
will be not declared.
What when my class is:
public class Test {
public enum Pro {
VALUE1,
VALUE2
}
public Pro p1;
}
And I want accept xmlDocument
, where field p1
is empty.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我希望第一个示例只是某种类型,因为它也有空的
b
。首先,并不是每个 XML 都可以反序列化为对象。特别是空元素是危险的,不应使用。如果您想表达b
未定义,则不要将其包含在 XML 文件中:或者使您的 b 属性可为空:
并将 XML 定义为:
通常,如果您想使用反序列化,请尝试首先使用序列化了解有效的 XML 必须是什么样子。
I expect that first example is just some type because it has empty
b
as well. First of all not every XML can be deserialized to object. Especially empty elements are dangerous and should not be used. If you want to express thatb
is not defined then do not include it in XML file:or make your b property nullable:
and define XML as:
Generally if you want to use deserialization try to first use serialization to understand how must a valid XML look like.