gil*_*ly3 5 c# xml-serialization
我有一些我正在使用和反序列化的XML.
<Foo>
<Bars Baz="9">
<Bar>...</Bar>
<Bar>...</Bar>
</Bars>
</Foo>
Run Code Online (Sandbox Code Playgroud)
目前我将它反序列化到这个类:
[XmlRoot("Foo")]
public class Foo
{
public Foo() { }
[XmlArrayItem("Bar")]
public Bar[] Bars { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
这工作正常,除了我没有捕获的值@Baz.我想添加Baz作为属性Foo,但我不知道如何.我将在我的Baz属性上设置什么属性来正确反序列化xml?
[WhatAttributeGoesHere("?")]
public int Baz { get; set; }
Run Code Online (Sandbox Code Playgroud)
一般:
[XmlAttribute]
Run Code Online (Sandbox Code Playgroud)
(使用可选的名称,命名空间等)就是你所追求的.
但是,您不能直接在集合上使用它.你需要有一个Bars的包装类,其属性和:
public class Foo {
public BarWrapper Bars {get;set;}
}
public class BarWrapper {
private readonly List<Bar> bars = new List<Bar>();
[XmlElement("Bar")]
public List<Bar> Items {get{return bars;}}
[XmlAttribute]
public int Baz {get;set;}
}
public class Bar {...}
Run Code Online (Sandbox Code Playgroud)