Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to deserialize this XML into an object

In other examples I've seen that are similar to mine, there is a root node, then an array node, and then a bunch of array items. My problem is, my root node is my array node, so examples I've seen don't seem to work for me, and I can't change the XML schema. Here's the XML:

<articles>  
    <article>
      <guid>7f6da9df-1a91-4e20-8b66-07ac7548dc47</guid>
      <order>1</order>
      <type>deal_abstract</type>
      <textType></textType>
      <id></id>
      <title>Abu Dhabi's IPIC Eyes Bond Sale After Cepsa Buy</title>
      <summary>Abu Dhabi's IPIC has appointed banks for a potential sterling and euro-denominated bond issue, a document showed on Wednesday, after the firm acquired Spain's Cepsa in a $5 billion deal earlier this month...</summary>
      <readmore></readmore>
      <fileName></fileName>
      <articleDate>02/24/2011 00:00:00 AM</articleDate>
      <articleDateType></articleDateType>
    </article>

    <article>
      <guid>1c3e57a0-c471-425a-87dd-051e69ecb7c5</guid>
      <order>2</order>
      <type>deal_abstract</type>
      <textType></textType>
      <id></id>
      <title>Big Law Abuzz Over New China Security Review</title>
      <summary>China’s newly established foreign investment M&amp;A review committee has been the subject of much legal chatter in the Middle Kingdom and beyond. Earlier this month, the State Council unveiled legislative guidance on…</summary>
      <readmore></readmore>
      <fileName></fileName>
      <articleDate>02/23/2011 00:00:00 AM</articleDate>
      <articleDateType></articleDateType>
    </article>  
</articles>

Here's my class:

public class CurrentsResultsList
{
    public Article[] Articles;
}

public class Article
{
    public string Guid { get; set; }
    public int Order { get; set; }
    public string Type { get; set; }
    public string Title { get; set; }
    public string Summary { get; set; }
    public DateTime ArticleDate { get; set; }
}

This is an XML response from an external API.

like image 740
Samo Avatar asked Mar 04 '11 19:03

Samo


Video Answer


1 Answers

  1. put it in a xml inside visual studio

  2. create the xsd schema

  3. use:

    "C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin\xsd.exe" "MyXsd.xsd" /t:lib /l:cs /c  /namespace:my.xsd /outputdir:"C:\testtttt"  
    

now you have your c# class ready.
and you can use this:

internal class ParseXML 
{
    public static xsdClass ToClass<xsdClass>(XElement ResponseXML)
    {
        return deserialize<xsdClass>(ResponseXML.ToString(SaveOptions.DisableFormatting));
    }

    private static result deserialize<result>(string XML)
    {
        using (TextReader textReader = new StringReader(XML))
        {
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(result));
            return (result) xmlSerializer.Deserialize(textReader);
        }
    } 
} 
like image 113
Fredou Avatar answered Sep 29 '22 00:09

Fredou