如果我有一个看起来像
的xml文件public func copyWithZone(zone: NSZone) -> AnyObject {
let copy = self.dynamicType.allocWithZone(zone) as ChartDataSet
copy.colors = colors
copy.label = self.label
return copy
}
我想把它变成一个看起来像
的C#类<Foo>
<Name>Some Data</Name>
<Bar_Data>Other Data</Bar_Data>
<Bar_MoreData>More Data</Bar_MoreData>
</Foo>
有没有办法通过简单的数据注释(public class Foo
{
public string Name {get; set; }
public Bar Bar { get; set; }
}
public class Bar
{
public string Data { get; set; }
public string MoreData { get; set; }
}
,XmlRoot
等)实现这一目标,或者是我实施XmlElement
的唯一选择?
编辑:注意,我只需要反序列化数据。我从第三方源获取XML,我不需要将IXmlSerializable
序列化回XML(如果这样更容易)。
答案 0 :(得分:4)
一种选择是使用XmlAnyElementAttribute,如下所示:
public class Foo
{
public string Name { get; set; }
public Bar Bar { get; set; }
[XmlAnyElementAttribute]
public XmlElement[] BarElements
{
get { return null; }
set
{
Bar = new Bar();
var barType = Bar.GetType();
foreach (var prop in value)
barType.GetProperty(prop.Name.Substring(4)).SetValue(Bar, prop.InnerText);
}
}
}
public class Bar
{
public string Data { get; set; }
public string MoreData { get; set; }
}
当XmlSerializer无法识别元素时,它会将其添加到由XmlAnyElementAttribute标记的XmlElement []类型的属性中。这是您可以处理Bar属性的地方。我在那里使用反射来表明这个想法。
另一种选择是反序列化两次并使用Foo连接Bar:
public class Foo
{
public string Name { get; set; }
public Bar Bar { get; set; }
}
[XmlRoot("Foo")]
public class Bar
{
[XmlElement("Bar_Data")]
public string Data { get; set; }
[XmlElement("Bar_MoreData")]
public string MoreData { get; set; }
}
var foo = (Foo) new XmlSerializer(typeof (Foo)).Deserialize(...);
var bar = (Bar) new XmlSerializer(typeof (Bar)).Deserialize(...);
foo.Bar = bar
另一种选择,没有入侵反序列化的类:
public class Foo
{
public string Name { get; set; }
public Bar Bar { get; set; }
}
public class Bar
{
public string Data { get; set; }
public string MoreData { get; set; }
}
var fooSerializer = new XmlSerializer(typeof (Foo));
fooSerializer.UnknownElement += (sender, e) =>
{
var foo = (Foo) e.ObjectBeingDeserialized;
if(foo.Bar == null)
foo.Bar = new Bar();
var propName = e.Element.Name.Substring(4);
typeof(Bar).GetProperty(propName).SetValue(foo.Bar, e.Element.InnerText);
};
var fooInstance = fooSerializer.Deserialize(...);
如果双重反序列化或反射在性能方面存在问题,那么您可以创建一个代理代理类:
[XmlRoot("Foo")]
public class FooSurrogate
{
public string Name { get; set; }
public string Bar_Data { get; set; }
public string Bar_MoreData { get; set; }
public Foo ToFoo()
{
return new Foo
{
Name = Name,
Bar = new Bar
{
Data = Bar_Data,
MoreData = Bar_MoreData
}
};
}
}
var seializer = new XmlSerializer(typeof (FooSurrogate));
var foo = ((FooSurrogate) seializer.Deserialize(...)).ToFoo();