이 요소 이름은 정말 수준에 따라 변경합니까? 그렇지 않다면 매우 간단한 클래스 모델 XmlSerializer
을 사용할 수 있습니다. IXmlSerializable
구현은 까다 롭습니다. 오류가 발생하기 쉽습니다. 반드시 사용해야하는 경우가 아니면 피하십시오. 이름은 다르지만 단단한 경우
, 난 그냥 XSD를 통해 실행하는 것 : XmlSerializer
를 들어
xsd example.xml
xsd example.xsd /classes
을 IXmlSerializable
예를하지 않고 (각 레벨에서 같은 이름을) :
using System;
using System.Collections.Generic;
using System.Xml.Serialization;
[XmlRoot("Foolist")]
public class Record
{
public Record(string name)
: this()
{
Name = name;
}
public Record() { Children = new List<Record>(); }
[XmlAttribute("name")]
public string Name { get; set; }
[XmlElement("Child")]
public List<Record> Children { get; set; }
}
static class Program
{
static void Main()
{
Record root = new Record {
Children = {
new Record("A") {
Children = {
new Record("Child 1"),
new Record("Child 2"),
}
}, new Record("B"),
new Record("C") {
Children = {
new Record("Child 1") {
Children = {
new Record("Little 1")
}
}
}
}}
};
var ser = new XmlSerializer(typeof(Record));
ser.Serialize(Console.Out, root);
}
}
(대답 덧글을) –