Windows Mobile 5 error read XML

Asked

Viewed 43 times

1

I am in debt to get the values of 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’t get the figures.

  • EmpData empty?

  • Windows Mobile :P.... recommend you to study windows phone 8

  • yes empty the list

  • I already use, but for PDA has to be this version

1 answer

0


You’re having two problems:

  1. You’re not specifying the namespace
  2. Descendents will find all descendants. Including the Vs within the Vs.

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 else:

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
    };

Browser other questions tagged

You are not signed in. Login or sign up in order to post.