c# - 使用序列化从 XML 文件读取到 C# 类

标签 c# xml

我有以下 XML 文件,我正在尝试使用 DE 序列化将其读入 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);
}

关于c# - 使用序列化从 XML 文件读取到 C# 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18340427/

相关文章:

c# - 如何在 Monotouch 中绑定(bind) NSObject<UIGestureRecognizerDelegate>?

c# - 什么类型的错误会导致程序慢慢使用更多的处理器能力并突然达到 100%?

c# - 除了创建助手之外,创建静态方法的理想情况(现实生活中的例子)是什么?

c# - Silverlight DataGrid 验证显示所有对象的验证错误|属性

c# - XML 中元素的子节点

c# - NSURLSession 下载任务 - Xamarin iOS F#

php - 使用 XSD 跨 XML 文档验证属性唯一性

c++ - Dom 与 Sax - 创建 Xml

c++ - 试图理解 GCC-XML 输出变量的含义

javascript - 如何在倒数第二条记录处停止JS循环? (xml数据)