反序列化的问题 [英] Problem with deserialization

查看:62
本文介绍了反序列化的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的xml:


<? xml version =" 1.0"编码= QUOT; UTF-8英寸;?> 
< test>
< row>
< col name =" W"> 14< / col>< col name =" P"> 63< / col>< col name =" G"> 01< / col>< col name =" MI"> 1< / col>
< / row>
< row>
< col name =" W"> 14< / col>< col name =" P"> 63< / col>< col name =" G"> 01< / col>< col name =" MI"> 1< / col>
< / row>
< / test>



我想将此xm反序列化为C#对象。我怎样才能创造出正确的阶级和deseriazlie呢?谢谢任何帮助。



我创建了这样的课程:


< pre class ="prettyprint"> [Serializable]
public class Col
{
[XmlAttribute(" name")]
public string name {get;组; }

[XmlText]
公共字符串值{get;组; }
}

[可序列化]
公共类行
{
[XmlElementAttribute(" col")]
public Col []山坳;
}

XmlSerializer serializer = new XmlSerializer(typeof(Row));
using(Stream stream = File.OpenRead(" C:\\ test.xml"))
{
Row rows =(Row)serializer.Deserialize(stream);
}




不幸的是它不起作用。

解决方案

您想使用已发布的这些特定类吗?或者你只需​​要创建一些C#代码来使用(反)序列化?在这种情况下,您可以使用
xsd.exe工具,它是.NET framework SDK首先从XML示例中推断出架构,然后从该架构生成C#类。


Hi, I've got xml like this:

<?xml version="1.0" encoding="utf-8"?>
<test>
<row>
<col name="W">14</col><col name="P">63</col><col name="G">01</col><col name="MI">1</col>
</row>
<row>
<col name="W">14</col><col name="P">63</col><col name="G">01</col><col name="MI">1</col>
</row>
</test>


I would like to deserialize this xm into C# object. How can I create right class and deseriazlie this? Thanx for any help.

I created classes like this:

[Serializable]
    public class Col
    {
        [XmlAttribute("name")]
        public string name { get; set; }

        [XmlText]
        public string value { get; set; }
    }

     [Serializable]
    public class Row
    {
         [XmlElementAttribute("col")]
         public Col[] col;
    }

XmlSerializer serializer = new XmlSerializer(typeof(Row));
            using (Stream stream = File.OpenRead("C:\\test.xml"))
            {
                Row rows = (Row)serializer.Deserialize(stream);
            }


Unfortunatelly it doesn't work.

解决方案

Do you want to use these particular classes you have posted? Or do you just need to create some C# code to work with (de)serialization? In that case you could use the xsd.exe tool that is part of the .NET framework SDK to infer a schema from your XML sample first and then to generate C# classes from that schema.


这篇关于反序列化的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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