2013-11-05 2 views
0

제목을 더 구체적으로 표현할 수 없다는 것에 사과하지만 예제를 통해 설명 할 수 있습니다.XML 직렬화 구조

나는 다음과 같은 XML로 직렬화 클래스를 구축하기 위해 노력하고있어

<Customize> 
    <Content></Content> 
    <Content></Content> 
    <!-- i.e. a list of Content --> 

    <Command></Command> 
    <Command></Command> 
    <Command></Command> 
    <!-- i.e. a list of Command --> 
</Customize> 

내 C 번호는 다음과 같습니다

[XmlRoot] 
public Customize Customize { get; set; } 

및 그러나

public class Customize 
{ 
    public List<Content> Content { get; set; } 
    public List<Command> Command { get; set; } 
} 

, 이것은 생산 (로 해야한다), 다음 :

<Customize> 
    <Content> 
     <Content></Content> 
     <Content></Content> 
    </Content> 
    <Command> 
     <Command></Command> 
     <Command></Command> 
     <Command></Command> 
    </Command> 
</Customize> 

원하는 xml을 달성하는 데 도움이되는 XML 직렬화 속성이 있습니까? 아니면 클래스를 작성하는 다른 방법을 찾아야합니까?

+0

당신이 직렬화 코드를 추가 할 수 있습니까? – Styxxy

답변

2

사용 XmlElementAttribute는 컬렉션 속성을 표시합니다.

public class Customize 
{ 
    [XmlElement("Content")] 
    public List<Content> Content { get; set; } 

    [XmlElement("Command")] 
    public List<Command> Command { get; set; } 
} 

빠른 테스트 코드 :

var item = new Customize() { Content = new List<Content> { new Content(), new Content() }, Command = new List<Command> { new Command(), new Command(), new Command() } }; 

string result; 

using (var writer = new StringWriter()) 
{ 
    var serializer = new XmlSerializer(typeof(Customize)); 
    serializer.Serialize(writer, item); 
    result = writer.ToString(); 
} 

인쇄 :

<?xml version="1.0" encoding="utf-16"?> 
<Customize xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <Content /> 
    <Content /> 
    <Command /> 
    <Command /> 
    <Command /> 
</Customize> 
1
public class Customize 
{ 
    [XmlElement("Content")] 
    public List<Content> Content { get; set; } 

    [XmlElement("Command")] 
    public List<Command> Command { get; set; } 
}