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

前端之家收集整理的这篇文章主要介绍了使用序列化从XML文件读入C#类前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有以下 XML文件,我试图使用DE-serialization读入c#中的类:
  1. <?xml version="1.0"?>
  2. <PropertiesMapping>
  3. <Property>
  4. <WEB_Class>InfoRequest</WEB_Class>
  5. <COM_Class>CInfoReq</COM_Class>
  6. <Mappings>
  7. <Map>
  8. <WEB_Property>theId</WEB_Property>
  9. <COM_Property>TheID</COM_Property>
  10. </Map>
  11. <Map>
  12. <WEB_Property>theName</WEB_Property>
  13. <COM_Property>NewName</COM_Property>
  14. </Map>
  15. </Mappings>
  16. </Property>
  17. </PropertiesMapping>

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

  1. PropertiesMapping pm = null;
  2.  
  3. try
  4. {
  5. System.IO.StreamReader str = new System.IO.StreamReader(@"PropertyMapping.xml");
  6. System.Xml.Serialization.XmlSerializer xSerializer = new System.Xml.Serialization.XmlSerializer(typeof(PropertiesMapping));
  7. pm = (PropertiesMapping)xSerializer.Deserialize(str);
  8. str.Close();
  9. }
  10. catch (Exception ex)
  11. {
  12. Console.WriteLine(ex.ToString());
  13. }
  14.  
  15.  
  16. [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true,Namespace = "")]
  17. [System.Xml.Serialization.XmlRootAttribute(Namespace = "",IsNullable = false)]
  18. public class PropertiesMapping
  19. {
  20. private string m_WEB_Class = "";
  21. private string m_COM_Class = "";
  22.  
  23. private List<IndividualProperties> m_EachProperty = null;
  24.  
  25. public string WEB_Class
  26. {
  27. get
  28. {
  29. return m_WEB_Class;
  30. }
  31. set
  32. {
  33. m_WEB_Class = value;
  34. }
  35. }
  36.  
  37. public string COM_Class
  38. {
  39. get
  40. {
  41. return m_COM_Class;
  42. }
  43. set
  44. {
  45. m_COM_Class = value;
  46. }
  47. }
  48.  
  49. public IndividualProperties GetIndividualProperties(int iIndex)
  50. {
  51. return m_EachProperty[iIndex];
  52. }
  53.  
  54. public void SetIndividualProperties(IndividualProperties theProp)
  55. {
  56. m_EachProperty.Add(theProp);
  57. }
  58. }
  59.  
  60.  
  61.  
  62. public class IndividualProperties
  63. {
  64. private string m_WEB_PropertyField;
  65.  
  66. private string m_COM_PropertyField;
  67.  
  68. public string WEB_Property
  69. {
  70. get
  71. {
  72. return this.m_WEB_PropertyField;
  73. }
  74. set
  75. {
  76. this.m_WEB_PropertyField = value;
  77. }
  78. }
  79.  
  80. public string COM_Property
  81. {
  82. get
  83. {
  84. return this.m_COM_PropertyField;
  85. }
  86. set
  87. {
  88. this.m_COM_PropertyField = value;
  89. }
  90. }
  91. }
您可以使用 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.

类别:

  1. [XmlType("PropertiesMapping")]
  2. public class PropertyMapping
  3. {
  4. public PropertyMapping()
  5. {
  6. Properties = new List<Property>();
  7. }
  8.  
  9. [XmlElement("Property")]
  10. public List<Property> Properties { get; set; }
  11. }
  12.  
  13. public class Property
  14. {
  15. public Property()
  16. {
  17. Mappings = new List<Mapping>();
  18. }
  19.  
  20. [XmlElement("WEB_Class")]
  21. public string WebClass { get; set; }
  22.  
  23. [XmlElement("COM_Class")]
  24. public string ComClass { get; set; }
  25.  
  26. [XmlArray("Mappings")]
  27. [XmlArrayItem("Map")]
  28. public List<Mapping> Mappings { get; set; }
  29. }
  30.  
  31. [XmlType("Map")]
  32. public class Mapping
  33. {
  34. [XmlElement("WEB_Property")]
  35. public string WebProperty { get; set; }
  36.  
  37. [XmlElement("COM_Property")]
  38. public string ComProperty { get; set; }
  39. }

演示:

  1. PropertyMapping result;
  2.  
  3. var serializer = new XmlSerializer(typeof(PropertyMapping));
  4.  
  5. using(var stream = new StringReader(data))
  6. using(var reader = XmlReader.Create(stream))
  7. {
  8. result = (PropertyMapping) serializer.Deserialize(reader);
  9. }

猜你在找的XML相关文章