2011-12-07 3 views
3

개체로 HTML 테이블을 deserialize하고 싶습니다. 그러나 다음 코드를 사용하면 <tr>의 부모로 <Rows>이 표시되고 <td>의 부모는 <Cells>으로 간주됩니다. 이 클래스 구조를 유지하고 싶습니다. 속성 선언이 누락 되었습니까?복수 부모 요소가없는 XML에서 목록을 어떻게 deserialize합니까?

<table> 
    <tr> 
    <td></td> 
    <td></td> 
    </tr> 
    <tr> 
    <td></td> 
    <td></td> 
    </tr> 
</table> 

public class Table 
{ 
    [XmlArrayItem("tr")] 
    public List<Row> Rows { get; set; } 
} 

public class Row 
{ 
    [XmlArrayItem("td")] 
    public List<Cell> Cells { get; set; } 
} 

public class Cell 
{ 
    public object Value { get; set; } 
} 
+0

가능한 복제 [내가 XmlSerializer가 컨테이너 태그를 직렬화하지 어떻게합니까?] (http://stackoverflow.com/questions/8199738/how-do-i-get-xmlserializer-to-not-serialize -container-tags) –

답변

4

아래와 같이 속성을 설정해보십시오. 클래스의 Value 유형을 object에서 string으로 변경해야합니다.

[XmlRoot("table")] 
public class Table 
{ 
    [XmlElement(typeof(Row), ElementName = "tr")] 
    public List<Row> Rows { get; set; } 
} 

public class Row 
{ 
    [XmlElement(typeof(Cell), ElementName = "td")] 
    public List<Cell> Cells { get; set; } 
} 

public class Cell 
{ 
    [XmlText(typeof(string))] 
    public string Value { get; set; } 
} 
+0

감사합니다. 이것은 내가 찾고 있었던 바로 그 것이다. – weilin8

+0

쿨, 다행스럽게 도울 수있어. – rsbarro

관련 문제