XML에 List<>
이 포함 된 클래스를 serialize하려는 C# .NET 3.5 응용 프로그램이 있습니다.List <> XML에 ICollection <>으로 내 보낸
Bar b1 = new Bar();
// populate b1 with interesting data
Bar b2 = new Bar();
// populate b2 with interesting data
Foo f = new Foo();
f.Bars.Add(b1);
f.Bars.Add(b2);
을 그리고 다음과 같이 직렬화 : :이처럼 그것을 채울 경우
[XmlRoot("Foo")]
class Foo
{
private List<Bar> bar_ = new List<Bar>();
private string something_ = "My String";
[XmlElement("Something")]
public string Something { get { return something_; } }
[XmlElement("Bar")]
public ICollection<Bar> Bars
{
get { return bar_; }
}
}
: 내 클래스는 다음과 같습니다
using (System.IO.TextWriter textWriter = new System.IO.StreamWriter(@"C:\foo.xml"))
{
System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(Foo));
serializer.Serialize(textWriter, f);
}
나는이처럼 보이는 파일을 가져 :
<Foo>
<Something>My String</Something>
</Foo>
하지만 원하는 것은 X입니다. 다음과 같습니다 ML :
나는List<>
가 XML에 표시하려면 어떻게해야합니까 무엇
<Foo>
<Something>My String</Something>
<Bar>
<!-- Data from first Bar -->
</Bar>
<Bar>
<!-- Data from second Bar -->
</Bar>
</Foo>
?
인터페이스를 'XmlSerialize'할 수 있다고 생각하지 않습니다. 어쨌든'ICollection'으로 직렬화하고 싶은 이유는 무엇입니까? 'List'으로 직렬화하고 소비자에게'ICollection '... ??? –
IAbstract
@IAbstract - 이해할 수 있는지 잘 모르겠습니다. '[XmlElement ("Bar")]'태그를 사용하여'private list bar_'을 표시하겠습니까? 그것은 출력을 변경하지 않습니다. 또한,'XmlSerializer' 문서는'IEnumerable'과'ICollection' 인터페이스 모두에서 작동한다는 것을 시사합니다. http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer%28v=VS.90%29.aspx –
PaulH
IAbstract가 가지고 있다고 생각합니다. 인터페이스를 직렬화 할 수 없습니다. 그래서 바 대신 ICollection이 바가되도록 List를 변경해야합니다 –