2010-10-06 9 views
1

Je suis en train de sérialiser un peu de XML Normalement, je voudrais juste créer une classe et utiliser le System.Xml.Serialization.XmlSerializer cependant, dans ce cas il y a plusieurs problèmes comme les quelques balises de lien qui ne sont différentes qu'à cause de la rel = 'Sérialiser XML où les éléments ne diffèrent que par rel = "

Et donc, je ne suis pas sûr de savoir comment le sérialiser et créer la classe pour cela.Toutes les idées? Voici un exemple de XML:

<?xml version="1.0" encoding="UTF-8"?> 
<feed xml:lang="en-US" xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/" xmlns="http://www.w3.org/2005/Atom"> 
    <id>tag:api.microsoft.com,2005:search/foo</id> 
    <title>foo - microsoft Search</title> 
    <link type="application/atom+xml" rel="next" href="http://api.microsoft.com/search?page=2&amp;q=foo" /> 
    <link type="application/atom+xml" rel="self" href="http://api.microsoft.com/search?page=1&amp;q=foo&amp;since_id=1286331268264" /> 
    <link type="application/atom+xml" rel="refresh" href="http://api.microsoft.com/search?q=foo&amp;since_id=1286331268264" /> 
    <link type="application/atom+xml" rel="after" href="http://api.microsoft.com/search?q=foo&amp;after_id=1286322320913" /> 
    <opensearch:totalResults>1173</opensearch:totalResults> 
    <opensearch:itemsPerPage>20</opensearch:itemsPerPage> 
    <updated>2010-10-06T02:14:28Z</updated> 

    <entry xmlns="http://www.w3.org/2005/Atom"> 
    <title>Comment on Eating in New Mexico Day 1 & 2 by Tricia</title> 
    <link href="http://zmommyt.wordpress.com/2007/08/28/eating-in-new-mexico-day-1-2/#comment-126" type="text/html" rel="alternate" /> 
    <published>2007-08-29T01:52:00Z</published> 
    <id>http://zmommyt.wordpress.com/2007/08/28/eating-in-new-mexico-day-1-2/#comment-126</id> 
    <updated>2007-08-29T01:52:00Z</updated> 
    <language xmlns='http://api.microsoft.com/ns/search-0#results'>blog</language> 
    <category xmlns='http://api.microsoft.com/ns/search-0#results'>blog</category> 
    <abstract xmlns='http://api.microsoft.com/ns/search-0#results'>> 
     <p>You&#8217;re making me hungry for green chile! I told my 
     farmer last week that I was already missing it - unfortunately, 
     his chiles won&#8217;t be ripe... ...Sigh&#8230; I love 
     that first picture! What great pictures. Comment on Eating in 
     New Mexico Day 1 & 2 by Tricia</p> 
    </abstract> 
    </entry> 

    <entry xmlns="http://www.w3.org/2005/Atom"> 
    <title>Comment on Eating in New Mexico Day 1 & 2 by Tricia</title> 
    <link href="http://zmommyt.wordpress.com/2007/08/28/eating-in-new-mexico-day-1-2/#comment-126" type="text/html" rel="alternate" /> 
    <published>2007-08-29T01:52:00Z</published> 
    <id>http://zmommyt.wordpress.com/2007/08/28/eating-in-new-mexico-day-1-2/#comment-126</id> 
    <updated>2007-08-29T01:52:00Z</updated> 
    <language xmlns='http://api.microsoft.com/ns/search-0#results'>blog</language> 
    <category xmlns='http://api.microsoft.com/ns/search-0#results'>blog</category> 
    <abstract xmlns='http://api.microsoft.com/ns/search-0#results'>> 
     <p>You&#8217;re making me hungry for green chile! I told my 
     farmer last week that I was already missing it - unfortunately, 
     his chiles won&#8217;t be ripe... ...Sigh&#8230; I love 
     that first picture! What great pictures. Comment on Eating in 
     New Mexico Day 1 & 2 by Tricia</p> 
    </abstract> 
    </entry> 
</feed> 

Répondre

2

Si vous appliquez la XmlElementAttribute à vos liens propriété de collection puis XmlSerializer leur désérialiser dans une collection ordonnée pour vous.

[XmlRoot(Namespace = "http://www.w3.org/2005/Atom")] 
public class feed 
{ 
    [XmlElement("link")] 
    public link[] link { get; set; } 
} 

public class link 
{ 
    [XmlAttribute] 
    public string type { get; set; } 

    [XmlAttribute] 
    public string rel { get; set; } 

    [XmlAttribute] 
    public string href { get; set; } 
}