将复杂的 XML 反序列化为 C# 对象
我有这种格式的 XML -
<Areas>
<Area>
<Property Name="Test11">a1</Property>
<Property Name="Test12">a2</Property>
<Property Name="Test13">a3</Property>
<Property Name="Test14">a4</Property>
<Property Name="Test15">a5</Property>
</Area>
<Area>
<Property Name="Test21">b1</Property>
<Property Name="Test22">b2</Property>
<Property Name="Test23">b3</Property>
<Property Name="Test24">b4</Property>
<Property Name="Test25">b5</Property>
</Area>
</Areas>
我使用 Microsoft 提供的 xsd.exe 生成了类 -
[Serializable()]
public partial class Areas
{
[XmlArrayItem("Property", typeof(AreasAreaProperty))]
public AreasAreaProperty[][] Area { get; set; }
}
[Serializable()]
public partial class AreasAreaProperty
{
[XmlAttribute()]
public string Name { get; set; }
[XmlText()]
public string Value { get; set; }
}
反序列化代码是 -
private void Deserialize()
{
XmlSerializer xs = new XmlSerializer(typeof(Areas));
FileStream fs = new FileStream("XMLFile1.xml", FileMode.Open);
XmlReader xr = new XmlTextReader(fs);
Areas a = (Areas)xs.Deserialize(xr);
fs.Close();
}
但在反序列化时,我收到此错误 - 无法将类型“AreasAreaProperty[]”转换为“AreasAreaProperty” 我在创建 XMLSerializer 对象时收到此错误。
怎么解决这个问题呢??提前致谢..
I have XML in this format -
<Areas>
<Area>
<Property Name="Test11">a1</Property>
<Property Name="Test12">a2</Property>
<Property Name="Test13">a3</Property>
<Property Name="Test14">a4</Property>
<Property Name="Test15">a5</Property>
</Area>
<Area>
<Property Name="Test21">b1</Property>
<Property Name="Test22">b2</Property>
<Property Name="Test23">b3</Property>
<Property Name="Test24">b4</Property>
<Property Name="Test25">b5</Property>
</Area>
</Areas>
I generated the class using xsd.exe provided by Microsoft as -
[Serializable()]
public partial class Areas
{
[XmlArrayItem("Property", typeof(AreasAreaProperty))]
public AreasAreaProperty[][] Area { get; set; }
}
[Serializable()]
public partial class AreasAreaProperty
{
[XmlAttribute()]
public string Name { get; set; }
[XmlText()]
public string Value { get; set; }
}
Code for deserializing is -
private void Deserialize()
{
XmlSerializer xs = new XmlSerializer(typeof(Areas));
FileStream fs = new FileStream("XMLFile1.xml", FileMode.Open);
XmlReader xr = new XmlTextReader(fs);
Areas a = (Areas)xs.Deserialize(xr);
fs.Close();
}
But at the time of deserilaization, i am getting this error -
Cannot convert type 'AreasAreaProperty[]' to 'AreasAreaProperty'
I am getting this error at the time of creation of object of XMLSerializer.
How to solve this issue?? Thanks in advance..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我想我以前见过这个。 XSD.exe 并不完美,因此您需要对结果进行一些修改。在以下代码中,在包含 [][] 的最后一行中,删除其中一个 [],使其成为“public AreasAreaProperty[] Area...”
I think I've seen this before. XSD.exe isn't perfect and so you need to tinker with the results a little bit. In the following code, on the last line where you have the [][], remove one of the [] so that it's "public AreasAreaProperty[] Area..."
我过去也遇到过类似的麻烦,看看这些问题的答案:
XML 反序列化为 XSD 生成的类时出现问题
.NET xsd 导入器创建不可序列化类
如果您了解您的架构,您应该尝试将其添加到 xsd,而不是将所有事情留给 xsd.exe 工具。
I've had similar troubles in the past, have a look at the answers to these:
Trouble with XML Deserialization into XSD generated classes
.NET xsd importer creates unserializable class
If you have knowledge about your schema you should try and add it to the xsd and not leave everything up to the xsd.exe tool.
您的根元素是.Deserialize() 方法的第四行不应该是
由于
.
Shouldn't the fourth line of your Deserialize() method be
instead of
since your root element is .