9

我有以下 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;
            }
        }
    }
4

3 回答 3

16

您可以使用XmlElementAttribute来简化您的 C# 命名。

指示当 XmlSerializer 序列化或反序列化包含它的对象时,公共字段或属性表示 XML 元素。

您将需要使用XmlArrayItemAttributeMappings元素。

表示一个属性,该属性指定 XmlSerializer 可以放置在序列化数组中的派生类型。

课程:

[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);
}
于 2013-08-20T16:48:48.573 回答
2

您不需要显式声明属性。只要确保名称匹配。唯一的非默认部分是[XmlArrayItem("Map")]您需要为地图数组使用不同名称的属性。

但是,您可以使用不同的名称,就像我对COM_PropertyWEB_Property通过指定XmlElementAttribute.

class Program
{
    static void Main(string[] args)
    {
        string testData = @"<?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>";

        var sr = new System.IO.StringReader(testData);
        var xs = new XmlSerializer(typeof(PropertiesMapping));

        object result = xs.Deserialize(sr);
    }
}

[Serializable]
public class PropertiesMapping
{
    public Property Property { get; set; }
}

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

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

    [XmlArrayItem("Map")]
    public Mapping[] Mappings { get; set; }
}

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

    [XmlElement("COM_Property")]
    public string ComProperty { get; set; }
}
于 2013-08-20T16:44:56.533 回答
0

根据您要实现的目标,使用XDocumentLinq-to-XML 可能更容易完成。

就序列化而言,这是一个适合您的类结构:

public class PropertiesMapping{
  [XmleElement]
  public string WEB_Class {get;set;}
  [XmleElement]
  public string COM_Class{get;set;}

  [XmlArray("Mappings")]
  public Map[] Mappings {get;set;}
}

public class Map{
  [XmleElement]
  public string WEB_Property{get;set;}
  [XmleElement]
  public string COM_Property{get;set;}
}
于 2013-08-20T16:45:04.923 回答