Read From XML File into C# Class Using Serialization

后端 未结 3 1399
不知归路
不知归路 2021-01-04 22:26

I have the following XML file that i am trying to read into a class in c# using DE-serialization:


             


        
相关标签:
3条回答
  • 2021-01-04 23:11

    You can use the XmlElementAttribute to simplify your C# naming.

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

    You will need to use XmlArrayItemAttribute for the Mappings element.

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

    Classes:

    [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; }
    }
    

    Demo:

    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);
    }
    
    0 讨论(0)
  • 2021-01-04 23:15

    Depending on what is it you're trying to achieve it may be done easier with XDocument and Linq-to-XML.

    As far as serializing goes, here's a class structure for you:

    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;}
    }
    
    0 讨论(0)
  • 2021-01-04 23:25

    You don't need to explicitly declare the properties. Just make sure the names match. The only non-default portion is the [XmlArrayItem("Map")] attribute you will need to use the different name for the map array.

    You can, however, use differing names as I have for the COM_Property and WEB_Property by specifying the 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; }
    }
    
    0 讨论(0)
提交回复
热议问题