使用序列化从XML文件读入C#类

我有以下 XML文件,我试图使用DE-serialization读入c#中的类:
<?xml version="1.0"?>
    <PropertiesMapping>
        <Property>
            <WEB_Class>InfoRequest</WEB_Class>
            <COM_Class>CInfoReq</COM_Class>
            <Mappings>
                <Map>
                    <WEB_Property>theId</WEB_Property>
                    <COM_Property>TheID</COM_Property>
                </Map>
                <Map>
                    <WEB_Property>theName</WEB_Property>
                    <COM_Property>NewName</COM_Property>
                </Map>
            </Mappings>
        </Property>
    </PropertiesMapping>

以下是我正在使用的代码,虽然它没有错误地执行,但没有数据被读入类PropertiesMapping,我在哪里错了?

PropertiesMapping pm = null;

        try
        {
            System.IO.StreamReader str = new System.IO.StreamReader(@"PropertyMapping.xml");
            System.Xml.Serialization.XmlSerializer xSerializer = new System.Xml.Serialization.XmlSerializer(typeof(PropertiesMapping));
            pm = (PropertiesMapping)xSerializer.Deserialize(str);
            str.Close();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }


[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true,Namespace = "")]
    [System.Xml.Serialization.XmlRootAttribute(Namespace = "",IsNullable = false)]
    public class PropertiesMapping
    {
        private string m_WEB_Class = "";
        private string m_COM_Class = "";

        private List<IndividualProperties> m_EachProperty = null;

        public string WEB_Class
        {
            get
            {
                return m_WEB_Class;
            }
            set
            {
                m_WEB_Class = value;
            }
        }

        public string COM_Class
        {
            get
            {
                return m_COM_Class;
            }
            set
            {
                m_COM_Class = value;
            }
        }

        public IndividualProperties GetIndividualProperties(int iIndex)
        {
            return m_EachProperty[iIndex];
        }

        public void SetIndividualProperties(IndividualProperties theProp)
        {
            m_EachProperty.Add(theProp);
        }
    }



public class IndividualProperties
    {
        private string m_WEB_PropertyField;

        private string m_COM_PropertyField;

        public string WEB_Property
        {
            get
            {
                return this.m_WEB_PropertyField;
            }
            set
            {
                this.m_WEB_PropertyField = value;
            }
        }

        public string COM_Property
        {
            get
            {
                return this.m_COM_PropertyField;
            }
            set
            {
                this.m_COM_PropertyField = value;
            }
        }
    }
您可以使用 XmlElementAttribute来简化C#命名.

Indicates that a public field or property represents an XML element when the XmlSerializer serializes or deserializes the object that contains it.

您将需要使用XmlArrayItemAttribute作为Mappings元素.

Represents an attribute that specifies the derived types that the XmlSerializer can place in a serialized array.

类别:

[XmlType("PropertiesMapping")]
public class PropertyMapping
{
    public PropertyMapping()
    {
        Properties = new List<Property>();
    }

    [XmlElement("Property")]
    public List<Property> Properties { get; set; }
}

public class Property
{
    public Property()
    {
        Mappings = new List<Mapping>();
    }

    [XmlElement("WEB_Class")]
    public string WebClass { get; set; }

    [XmlElement("COM_Class")]
    public string ComClass { get; set; }

    [XmlArray("Mappings")]
    [XmlArrayItem("Map")]
    public List<Mapping> Mappings { get; set; }
}

[XmlType("Map")]
public class Mapping
{
    [XmlElement("WEB_Property")]
    public string WebProperty { get; set; }

    [XmlElement("COM_Property")]
    public string ComProperty { get; set; }
}

演示:

PropertyMapping result;

var serializer = new XmlSerializer(typeof(PropertyMapping));

using(var stream = new StringReader(data))
using(var reader = XmlReader.Create(stream))
{
    result = (PropertyMapping) serializer.Deserialize(reader);
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念
xml文件介绍及使用
xml编程(一)-xml语法
XML文件结构和基本语法
第2章 包装类
XML入门的常见问题(二)
Java对象的强、软、弱和虚引用
JS解析XML文件和XML字符串详解
java中枚举的详细使用介绍
了解Xml格式
XML入门的常见问题(四)
深入SQLite多线程的使用总结详解
PlayFramework完整实现一个APP(一)
XML和YAML的使用方法
XML轻松学习总节篇