2011-10-20 9 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; } 
} 

ですされているが、位置に、私は私が間違っているところヌルが誰を私に伝えることができます取得しています。

配列( IListICollectionなど)の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