开发者

XDocument wont get Descendants although the chosen one exists

im having kinda trouble with a XDocument create the XDocument parsing an xml returned from a webservice. Here is the xml

<VentaOnlineList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/"> 
  <VentasList> 
    <VentaOnlineInfo> 
      <ProcessDate>2010-11-01T00:00:00</ProcessDate> 
      <TicketDate>2010-11-01T00:00:00</TicketDate> 
      <DeliveryDate>2010-09-29T00:00:00</DeliveryDate> 
      <DwhLastMonthProcessData>0</DwhLastMonthProcessData> 
      <DwhLastMonthTicketDate>0</DwhLastMonthTicketDate> 
      <PsucLastMonthDeliveryDate>0</PsucLastMonthDeliveryDate> 
      <DwhSelectedMonthProcessData>-6.54</DwhSelectedMonthProcessData> 
      <DwhSelectedMonthTicketDate>-6.54</DwhSelectedMonthTicketDate> 
    开发者_运维技巧  <PsucSelectedMonthDeliveryDate>-6.54</PsucSelectedMonthDeliveryDate> 
      <DwhNextMonthProcessData>0</DwhNextMonthProcessData> 
      <DwhNextMonthTicketDate>0</DwhNextMonthTicketDate> 
      <PsucNextMonthDeliveryDate>0</PsucNextMonthDeliveryDate> 
    </VentaOnlineInfo>  
  </VentasList> 
  <Error> 
    <ErrorFlag>false</ErrorFlag> 
  </Error> 
</VentaOnlineList>

now, when i try to call the Descendants lets say from "VentaOnlineInfo" it says that its empty.

here is the code

XDocument xmlSell = XDocument.Parse(xmlContent); 
        XNamespace nameSpace = "http://tempuri.org/";

        var venta = from ventas in xmlSell.Descendants(nameSpace + "VentaOnlineInfo")
                    select new VentaDigital
                    {
                        ProcessDate = (DateTime)ventas.Attribute("ProcessDate"),
                        TicketDate = (DateTime)ventas.Attribute("TicketDate"),
                        DeliveryDate = (DateTime)ventas.Attribute("DeliveryDate")
                    };

        ventasDigitales.ItemsSource = venta;

xmlContent its filled with a string that contains the XML.

Somebody knows why it keeps saying that the descendant dont exist? Any help will be apreciated


You have a few issues here.

First, it looks like you put Attribute when you meant Element in the select clause. Second, you need to include the namespace when getting those Elements. Third, you cannot cast to a DateTime you must parse it. (EDIT: as long as you don't explicitly reference the Element's Value property it will cast)

Here's the code you posted with those issues fixed. It appears to work for me:

XDocument xmlSell = XDocument.Parse(x); 
    XNamespace nameSpace = "http://tempuri.org/";

    var venta = from ventas in xmlSell.Descendants(nameSpace + "VentaOnlineInfo")
                select new VentaDigital
                {
                    ProcessDate = (DateTime)ventas.Element(nameSpace + "ProcessDate"),
                    TicketDate = (DateTime)ventas.Element(nameSpace + "TicketDate"),
                    DeliveryDate = (DateTime)ventas.Element(nameSpace + "DeliveryDate")
                };

        ventasDigitales.ItemsSource = venta;


You need to fetch them subvalues as elements and include the namespace. Here's a short but complete example which works:

using System;
using System.Linq;
using System.Xml.Linq;

class Test
{
    static void Main()        
    {
        string xml = @"<VentaOnlineList 
                          xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' 
                          xmlns:xsd='http://www.w3.org/2001/XMLSchema' 
                          xmlns='http://tempuri.org/'> 
  <VentasList> 
    <VentaOnlineInfo> 
      <ProcessDate>2010-11-01T00:00:00</ProcessDate> 
      <TicketDate>2010-11-01T00:00:00</TicketDate> 
      <DeliveryDate>2010-09-29T00:00:00</DeliveryDate> 
      <DwhLastMonthProcessData>0</DwhLastMonthProcessData> 
      <DwhLastMonthTicketDate>0</DwhLastMonthTicketDate> 
      <PsucLastMonthDeliveryDate>0</PsucLastMonthDeliveryDate> 
      <DwhSelectedMonthProcessData>-6.54</DwhSelectedMonthProcessData> 
      <DwhSelectedMonthTicketDate>-6.54</DwhSelectedMonthTicketDate> 
      <PsucSelectedMonthDeliveryDate>-6.54</PsucSelectedMonthDeliveryDate> 
      <DwhNextMonthProcessData>0</DwhNextMonthProcessData> 
      <DwhNextMonthTicketDate>0</DwhNextMonthTicketDate> 
      <PsucNextMonthDeliveryDate>0</PsucNextMonthDeliveryDate> 
    </VentaOnlineInfo>  
  </VentasList> 
  <Error> 
    <ErrorFlag>false</ErrorFlag> 
  </Error> 
</VentaOnlineList>";

        XDocument xmlSell = XDocument.Parse(xml);
        XNamespace nameSpace = "http://tempuri.org/";

        var venta = from ventas in xmlSell.Descendants(nameSpace + "VentaOnlineInfo")
                    select new
                    {
                        ProcessDate = (DateTime)ventas.Element(nameSpace + "ProcessDate"),
                        TicketDate = (DateTime)ventas.Element(nameSpace + "TicketDate"),
                        DeliveryDate = (DateTime)ventas.Element(nameSpace + "DeliveryDate")
                    };

        foreach (var x in venta)
        {
            Console.WriteLine(x);
        }
    }
}

Now if you do that and you're still not getting anything, that would suggest that something odd is happening in your binding.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜