locked
linq to xml RRS feed

  • Question

  • Hi, i've a problem with my astoria project.
    i've created a db named SalesOrderCompany and from this the  entity model of the db that it's used in the web service of the astoria project. i would like to display in a silverlight datagrid the result of the query  (select customer):
    "http://localhost:50527/SalesCompanyService.svc/Customer?$orderby=Cognome"

    following the instruction provided there: http://weblogs.asp.net/scottgu/pages/silverlight-tutorial-part-3-using-networking-to-retrieve-data-and-populate-a-datagrid.aspx
    i've associated a silverlight project to the "astoria" one but in order to set the item source  of the datagrid i've to parse in linq the following xml:

    <feed xml:base="http://localhost:50527/SalesCompanyService.svc/" xmlns:ads="http://schemas.microsoft.com/ado/2007/08/dataweb" xmlns:adsm="http://schemas.microsoft.com/ado/2007/08/dataweb/metadata" xmlns="http://www.w3.org/2005/Atom">
      <id>http://localhost:50527/SalesCompanyService.svc/Customer</id>
      <updated />
      <title>Customer</title>
      <link rel="self" href="Customer" title="Customer" />
      <entry adsm:type="SalesCompanyModel.Customer">
        <id>http://localhost:50527/SalesCompanyService.svc/Customer(4)</id>
        <updated />
        <title />
        <author>
          <name />
        </author>
        <link rel="edit" href="Customer(4)" title="Customer" />
        <content type="application/xml">
          <adsTongue TiedalesOrderID adsm:type="Int32">4</adsTongue TiedalesOrderID>
          <ads:Nome>******</ads:Nome>
          <ads:Cognome>******</ads:Cognome>
          <ads:Indirizzo>Viale Pelleia</ads:Indirizzo>
          <ads:Città>Genova</ads:Città>
          <adsStick out tongueaese>Italia</adsStick out tongueaese>
          <ads:Zip>16147</ads:Zip>
          <ads:Telefono>3380980789</ads:Telefono>
        </content>
        <link rel="related" title="SalesOrder" href="Customer(4)/SalesOrder" type="application/atom+xml;type=entry" />
      </entry>
      <entry adsm:type="SalesCompanyModel.Customer">
        <id>http://localhost:50527/SalesCompanyService.svc/Customer(0)</id>
        <updated />
        <title />
        <author>
          <name />
        </author>
        <link rel="edit" href="Customer(0)" title="Customer" />
        <content type="application/xml">
          <adsTongue TiedalesOrderID adsm:type="Int32">0</adsTongue TiedalesOrderID>
          <ads:Nome>******</ads:Nome>
          <ads:Cognome>******</ads:Cognome>
          <ads:Indirizzo>Via Capelloni 6</ads:Indirizzo>
          <ads:Città>Sanremo</ads:Città>
          <adsStick out tongueaese>Italia</adsStick out tongueaese>
          <ads:Zip>23456</ads:Zip>
          <ads:Telefono>3338478921</ads:Telefono>
        </content>
        <link rel="related" title="SalesOrder" href="Customer(0)/SalesOrder" type="application/atom+xml;type=entry" />
      </entry>
      <entry adsm:type="SalesCompanyModel.Customer">
        <id>http://localhost:50527/SalesCompanyService.svc/Customer(2)</id>
        <updated />
        <title />
        <author>
          <name />
        </author>
        <link rel="edit" href="Customer(2)" title="Customer" />
        <content type="application/xml">
          <adsTongue TiedalesOrderID adsm:type="Int32">2</adsTongue TiedalesOrderID>
          <ads:Nome>******</ads:Nome>
          <ads:Cognome>******</ads:Cognome>
          <ads:Indirizzo>Piazza Fattoni 3</ads:Indirizzo>
          <ads:Città>Genova</ads:Città>
          <adsStick out tongueaese>Italia</adsStick out tongueaese>
          <ads:Zip>16147</ads:Zip>
          <ads:Telefono>3459098098</ads:Telefono>
        </content>
        <link rel="related" title="SalesOrder" href="Customer(2)/SalesOrder" type="application/atom+xml;type=entry" />
      </entry>
      <entry adsm:type="SalesCompanyModel.Customer">
        <id>http://localhost:50527/SalesCompanyService.svc/Customer(1)</id>
        <updated />
        <title />
        <author>
          <name />
        </author>
        <link rel="edit" href="Customer(1)" title="Customer" />
        <content type="application/xml">
          <adsTongue TiedalesOrderID adsm:type="Int32">1</adsTongue TiedalesOrderID>
          <ads:Nome>******</ads:Nome>
          <ads:Cognome>******</ads:Cognome>
          <ads:Indirizzo>Via Zorco 12</ads:Indirizzo>
          <ads:Città>Amsterdam</ads:Città>
          <adsStick out tongueaese>Olanda</adsStick out tongueaese>
          <ads:Zip>67899</ads:Zip>
          <ads:Telefono>3456780098</ads:Telefono>
        </content>
        <link rel="related" title="SalesOrder" href="Customer(1)/SalesOrder" type="application/atom+xml;type=entry" />
      </entry>
      <entry adsm:type="SalesCompanyModel.Customer">
        <id>http://localhost:50527/SalesCompanyService.svc/Customer(3)</id>
        <updated />
        <title />
        <author>
          <name />
        </author>
        <link rel="edit" href="Customer(3)" title="Customer" />
        <content type="application/xml">
          <adsTongue TiedalesOrderID adsm:type="Int32">3</adsTongue TiedalesOrderID>
          <ads:Nome>******</ads:Nome>
          <ads:Cognome>******</ads:Cognome>
          <ads:Indirizzo>Via Belin</ads:Indirizzo>
          <ads:Città>Milano</ads:Città>
          <adsStick out tongueaese>Italia</adsStick out tongueaese>
          <ads:Zip>16456</ads:Zip>
          <ads:Telefono>3456789028</ads:Telefono>
        </content>
        <link rel="related" title="SalesOrder" href="Customer(3)/SalesOrder" type="application/atom+xml;type=entry" />
      </entry>
    </feed>

    i've tried with this:
    (the string xmlContent contains the xml above)
    System.Collections.IEnumerable retrieveData(string xmlContent)
            {
                XDocument xml = XDocument.Parse(xmlContent);
               
                var customers = from customer in xml.Descendants("Customer")
                                select new Customer
                                {
                                    SalesOrderID = (int)customer.Attribute("SalesOrderID"),
                                    Nome = (string)customer.Attribute("Nome"),
                                    Cognome = (string)customer.Attribute("Cognome"),
                                    Indirizzo = (string)customer.Attribute("Indirizzo"),
                                    Città = (string)customer.Attribute("Città"),
                                    Paese = (string)customer.Attribute("Paese"),
                                    Zip = (string)customer.Attribute("Zip"),
                                    Telefono = (string)customer.Attribute("Telefono")
                                };
               
                return customers;

            }
    but it doesn't seem to work. i'm not an expert about xml so i can't think of another method to retrieve the customers from this xml...Any help will be greatly appreciated
    Monday, May 12, 2008 8:45 PM

Answers

  • In the next release of Silverlight (beta2 SDK) we will provide a client library which makes interacting with services much easier (no hand parsing XML). 

    Sunday, May 18, 2008 3:47 AM
  • This works for me:

     

    XDocument xml = XDocument.Parse(xmlContent);
               
                var customers = from customer in xml.Document.Descendants("Customer")
                                select new Customer
                                {
                                    SalesOrderID = (int)customer.Attribute("SalesOrderID"),
                                    Nome = (string)customer.Attribute("Nome"),
                                    Cognome = (string)customer.Attribute("Cognome"),
                                    Indirizzo = (string)customer.Attribute("Indirizzo"),
                                    Città = (string)customer.Attribute("Città"),
                                    Paese = (string)customer.Attribute("Paese"),
                                    Zip = (string)customer.Attribute("Zip"),
                                    Telefono = (string)customer.Attribute("Telefono")
                                };
               
                return customers;

     

    Tuesday, May 27, 2008 9:26 PM

All replies

  • In the next release of Silverlight (beta2 SDK) we will provide a client library which makes interacting with services much easier (no hand parsing XML). 

    Sunday, May 18, 2008 3:47 AM
  • This works for me:

     

    XDocument xml = XDocument.Parse(xmlContent);
               
                var customers = from customer in xml.Document.Descendants("Customer")
                                select new Customer
                                {
                                    SalesOrderID = (int)customer.Attribute("SalesOrderID"),
                                    Nome = (string)customer.Attribute("Nome"),
                                    Cognome = (string)customer.Attribute("Cognome"),
                                    Indirizzo = (string)customer.Attribute("Indirizzo"),
                                    Città = (string)customer.Attribute("Città"),
                                    Paese = (string)customer.Attribute("Paese"),
                                    Zip = (string)customer.Attribute("Zip"),
                                    Telefono = (string)customer.Attribute("Telefono")
                                };
               
                return customers;

     

    Tuesday, May 27, 2008 9:26 PM