Search code examples
c#xmlwindows-phone-7xml-serialization

Get values from an xml (4 levels)


Help for values ​​in a xml at various levels.

This is the xml:

<widgets>
    <id>95</id>

    <widget type="1" name="accventas" caption="Ofertas ventas" flags="4">
        <service name="pm_venofer_total00001" caption="Pendientes de aceptar" desc="" type="3" detail="1">
            <xvalue>20</xvalue>
            <xcolor>1</xcolor>
        </service>
    </widget>

    <widget type="3" name="today_state" caption="Estado de ventas" flags="4">
        <service name="pd_today_orders00001" caption="Pedidos" desc="Nº pedidos del día" type="3" detail="1">
            <xvalue>0</xvalue>
            <xcolor>2</xcolor>
            <xalert>No se está vendiendo nada</xalert>
        </service>

        <service name="pd_today_sales00001" caption="Importe" desc="Importe ventas del día" type="3" detail="1">
            <xvalue>0,00</xvalue>
            <xcolor>2</xcolor>
            <xalert>No estamos recaudando nada</xalert>
        </service>
    </widget>
</widgets>

Loaded the xml and is ready to be tried, but I can not get all the fields you need

I need:

  • id,
  • caption attribute of the widget,
  • the services of each widget,
  • the caption attribute of service,
  • xValue,
  • xcolor and xalert,
  • each service

I get to get all the widgets, like this: (I think two kinds: Employees and Employee)

[XmlRoot("widgets")]
public class Employees
{
    [XmlElement("widget")]
    public ObservableCollection <Employee> Coleccion { get; set; }
}


 public class Employee
 {
    [XmlAttribute("caption")]
    public string nombreWidget { get; set; }
 }

But not like getting inside each widget their respective services (service attribute), and within these xValue, xcolor and xalert


Solution

  • LinqToXml solution:

    var xml = XDocument.Parse(Resource1.XMLFile1).Root;
    var parsed = new {
                         Id = xml.Element("id").Value,
                         Widgets = xml.Elements("widget")
                                      .Select(w => new
                                      {
                                          Caption = w.Attribute("caption").Value,
                                          Services = w.Elements("service").Select(s => new
                                          {
                                              Caption = s.Attribute("caption").Value,
                                              XColor = s.Element("xcolor").Value,
                                              XValue = s.Element("xvalue").Value,
                                              XAlert = s.Element("xalert") != null ? s.Element("xalert").Value : null
                                          }).ToList()
                                      }).ToList()
                     };
    

    It will creates anonymous objects representing your inpout XML. You can easily replace that anonymous objects from my code with your real domain objects (Employees etc.).