2011-10-20 2 views
3

내 XML은XML 역 직렬화 문제가

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<person> 
    <id>sKQ0F4h1ft</id> 
    <first-name>Govind</first-name> 
    <last-name>Malviya</last-name> 
    <positions total="3"> 
    <position> 
     <id>sdfsdfsf</id> 
     <title>Founder &amp; CEO</title> 
     <summary>fsdsdf</summary> 
     <start-date> 
     <year>2010</year> 
     <month>12</month> 
     </start-date> 
     <is-current>true</is-current> 
     <company> 
     <name>sdfsdf</name> 
     <industry>Internet</industry> 
     </company> 
    </position> 
    <position> 
     <id>17908sdfsdf4226</id> 
     <title>Engineer-in-traning</title> 
     <summary></summary> 
     <start-date> 
     <year>2010</year> 
     <month>3</month> 
     </start-date> 
     <is-current>true</is-current> 
     <company> 
     <id>sdfsdf</id> 
     <name>sdfsdf</name> 
     <industry>sfsdf sdfs sdf </industry> 
     </company> 
    </position> 
    <position> 
     <id>sdfsdff</id> 
     <title>Graduate Researcher</title> 
     <summary></summary> 
     <start-date> 
     <year>2006</year> 
     <month>8</month> 
     </start-date> 
     <end-date> 
     <year>2009</year> 
     <month>1</month> 
     </end-date> 
     <is-current>false</is-current> 
     <company> 
     <id>sdfsdf</id> 
     <name>University of Alberta</name> 
     <type>Educational Institution</type> 
     <industry>Higher Education</industry> 
     </company> 
    </position> 
    </positions> 
</person> 

클래스가

[Serializable, XmlRoot("person")] 
public class FooUserProfile 
{ 
    [XmlElement("id")] 
    public string ID { get; set; } 

    [XmlElement("first-name")] 
    public string FirstName { get; set; } 

    [XmlElement("last-name")] 
    public string LastName { get; set; } 


    [XmlElement("positions")] 
    public List<FooPosition> Positions { get; set; } 
} 

[Serializable] 
public class FooPosition 
{ 
    [XmlElement("id")] 
    public string ID { get; set; } 

    [XmlElement("title")] 
    public string Title { get; set; } 

    [XmlElement("summary")] 
    public string Summary { get; set; } 

    [XmlElement("start-date")] 
    public FooDate StartDate { get; set; } 

    [XmlElement("end-date")] 
    public FooDate EndDate { get; set; } 

    [XmlElement("is-current")] 
    public string IsCurrent { get; set; } 

    [XmlElement("company")] 
    public FooPositionCompany Company { get; set; } 
} 

[Serializable] 
public class FooDate 
{ 
    [XmlElement("year")] 
    public string Year { get; set; } 

    [XmlElement("month")] 
    public string Month { get; set; } 
} 

[Serializable] 
public class FooPositionCompany 
{ 
    [XmlElement("id")] 
    public string ID { get; set; } 

    [XmlElement("name")] 
    public string Name { get; set; } 

    [XmlElement("type")] 
    public string Type { get; set; } 

    [XmlElement("industry")] 
    public string Industry { get; set; } 
} 

을하지만 위치에 나는 내가 잘못 여기서 널 아무도 말해 줄 수 얻고있다.

배열 ( IList, ICollection 등)의 XML 요소의 이름을 지정하기 위해

답변

10

과 그것의 항목, 당신은 속성을 사용할 필요가 XmlArrayXmlArrayItem :

[Serializable, XmlRoot("person")] 
public class FooUserProfile 
{ 
    /* The other members... */ 


    [XmlArray("positions")] 
    [XmlArrayItem("position")] 
    public List<FooPosition> Positions { get; set; } 
} 

이 속성 XmlElement는 효과가있다

[XmlRoot("Config")] 
public class Foo 
{ 
    [XmlElement("Id")] 
    public string[] IdStringArrayWithStupidName; 
} 

직렬화 된 XML :

주변 XML 배열 요소는 생략하고 이름을의하는 XML 배열 항목을 제공 할 것이다
<?xml version="1.0" encoding="?> 
<Config> 
    <Id></Id> 
    <Id></Id> 
</Config> 
+0

덕분에 제대로 작동합니다. –

+0

'total = "3"'을 어떻게 포착하겠습니까? 또는 목록 태그의 다른 속성? –

+0

XmlAttributeAttribute를 찾으십시오. – Stefan