我有以下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>
Run Code Online (Sandbox Code Playgroud)
以下是我正在使用的代码,虽然它没有错误地执行,但没有数据被读入类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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
Dus*_*gen 12
您可以使用它XmlElementAttribute来简化您的C#命名.
指示当XmlSerializer序列化或反序列化包含它的对象时,公共字段或属性表示XML元素.
您将需要使用XmlArrayItemAttribute的Mappings元素.
表示一个属性,指定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; }
}
Run Code Online (Sandbox Code Playgroud)
演示:
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);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
26118 次 |
| 最近记录: |