sie*_*z0r 6 c# collections camelcasing xml-serialization wrapper
我需要在驼峰套管中导出一组物品,为此我使用了一个包装.
班级本身:
[XmlRoot("example")]
public class Example
{
[XmlElement("exampleText")]
public string ExampleText { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
这序列化很好:
<example>
<exampleText>Some text</exampleText>
</example>
Run Code Online (Sandbox Code Playgroud)
包装:
[XmlRoot("examples")]
public class ExampleWrapper : ICollection<Example>
{
[XmlElement("example")]
public List<Example> innerList;
//Implementation of ICollection using innerList
}
Run Code Online (Sandbox Code Playgroud)
然而,Example由于某种原因,这会将包裹的s 大写,我试图用它来覆盖它,XmlElement但这似乎没有达到预期的效果:
<examples>
<Example>
<exampleText>Some text</exampleText>
</Example>
<Example>
<exampleText>Another text</exampleText>
</Example>
</examples>
Run Code Online (Sandbox Code Playgroud)
谁能告诉我我做错了什么或者有更简单的方法?
问题是XmlSerializer对集合类型进行了内置处理,这意味着它将忽略所有属性和字段(包括innerList),如果您的类型恰好实现ICollection,并将根据自己的规则对其进行序列化.但是,您可以使用该XmlType属性自定义其用于集合项的元素的名称(与XmlRoot您在示例中使用的元素相对):
[XmlType("example")]
public class Example
{
[XmlElement("exampleText")]
public string ExampleText { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
这将具有所需的序列化.
请参阅http://msdn.microsoft.com/en-us/library/ms950721.aspx,特别是问题的答案"为什么集合类的所有属性都没有序列化?"
| 归档时间: |
|
| 查看次数: |
497 次 |
| 最近记录: |