Windows Mobile 5 error reading XML

1

I'm having diviculties in getting the values from the following XML

    <ArrayOf    xmlns="http://schemas.datacontract.org/2004/07/WcfServicePedido_v7"    xmlns:i="http://www.w3.org/2001/XMLSchema-instance"> 
<V>    
<D>S</D> 
<I>Z</I> 
<V>1</V> </V> <V>    
<D>S</D>    
<I>Z</I> 
<V>2</V> </V>    
</ArrayOf>

To read I used the following code:

XmlReader xmlReader = XmlReader.Create(url);

            XDocument x = XDocument.Load(xmlReader);




            var EmpData = from emp in x.Descendants("V")
                          select new M
                          {
                              V = Convert.ToInt32(emp.Descendants("V").First().Value),
                              I = emp.Descendants("I").First().Value,
                              D = emp.Descendants("D").First().Value
                          };

            List<M> aux = EmpData.ToList();

I can not get the values.

    
asked by anonymous 07.03.2014 / 20:13

1 answer

0

You're having two problems:

  • You are not specifying the namespace
  • Descendents will find all descendants. Including V s within V s.
  • Try it like this:

    var EmpData = from emp in x.Root.Elements("{http://schemas.datacontract.org/2004/07/WcfServicePedido_v7}V")
        select new 
        {
            V = XmlConvert.ToInt32(emp.Element("{http://schemas.datacontract.org/2004/07/WcfServicePedido_v7}V").Value),
            I = emp.Element("{http://schemas.datacontract.org/2004/07/WcfServicePedido_v7}I").Value,
            D = emp.Element("{http://schemas.datacontract.org/2004/07/WcfServicePedido_v7}D").Value
        };
    

    Or:

    var ns = XNamespace.Get("http://schemas.datacontract.org/2004/07/WcfServicePedido_v7");
    
    var EmpData = from emp in x.Root.Elements(ns+"V")
        select new 
        {
            V = XmlConvert.ToInt32(emp.Element(ns+"V").Value),
            I = emp.Element(ns+"I").Value,
            D = emp.Element(ns+"D").Value
        };
    

    Or:

    var ns = XNamespace.Get("http://schemas.datacontract.org/2004/07/WcfServicePedido_v7");
    var vName = ns + "V";
    var iName = ns + "I";
    var dName = ns + "D";
    
    var EmpData = from emp in x.Root.Elements(vName)
        select new 
        {
            V = XmlConvert.ToInt32(emp.Element(vName).Value),
            I = emp.Element(iName).Value,
            D = emp.Element(dName).Value
        };
    
        
    09.03.2014 / 23:42