Sunday, 8 February 2015

Iterating an XML document using XDocument only return 1 descendant



I am struggling with the XDocument class. I have an XML document which has "produkter", and then a long list of children (1000+).


However, when I call xdoc.Descendants("produkter").Count(), the length is only 1.


My code:



public void Transform()
{
var path = HttpContext.Current.Server.MapPath("~/Content/Feeds/produkter-partnerid13389-Bianco.dk.xml");

XDocument xdoc = XDocument.Load(path);

foreach (XElement xe in xdoc.Descendants("produkter"))
{
if (xe == null)
{
continue;
}

var xer = xe.Element("produkt");

var productId = xer.Element("produktid").Value;

var biancoProd = getBiancoPoco(xer);

// create
var product = _productService.GetProductById(productId, supplierId);
if (product == null)
{
Product pr = new Product();
setProductByPoco(pr,biancoProd);
_productService.Create(pr);

Debug.WriteLine("Creating new product: {0}",pr.Guid);
}
// update
else
{
setProductByPoco(product, biancoProd);
_productService.Update(product);
}
}
}


Feed content:



<?xml version="1.0" encoding="iso-8859-1"?>
<produkter>

<produkt><forhandler>Bianco.dk</forhandler>
<kategorinavn>Til Hende - Sko - Ballerina|Til Hende - Sko - Flade sko</kategorinavn>
<produktnavn>Laced Up Shoe DJF15</produktnavn>
<produktid>532683</produktid>
<beskrivelse>Canvas sko med snørelukning og kraftig sål fra Bianco</beskrivelse>
<nypris>399.00</nypris>
<billedurl>http://ift.tt/1KwqSOi;
<vareurl>http://ift.tt/1IuPE42;
</produkt>

<produkt><forhandler>Bianco.dk</forhandler>
<kategorinavn>Til Hende - Sko - Party|Til Hende - Sko - Stiletter|Til Hende - Sko - Pumps</kategorinavn>
<produktnavn>Nutopa Pump DJF15</produktnavn>
<produktid>532674</produktid>
<beskrivelse>Klassisk pump med medium hælhøjde og plateau fra Bianco</beskrivelse>
<nypris>449.00</nypris>
<billedurl>http://ift.tt/1IuPEki;
<vareurl>http://ift.tt/1KwqSOk;
</produkt>

<produkt><forhandler>Bianco.dk</forhandler>
<kategorinavn>Til Hende - Sko - Party|Til Hende - Sko - Stiletter|Til Hende - Sko - Pumps</kategorinavn>
<produktnavn>Nutopa Pump DJF15</produktnavn>
<produktid>532674</produktid>
<beskrivelse>Klassisk pump med medium hælhøjde og plateau fra Bianco</beskrivelse>
<nypris>449.00</nypris>
<billedurl>http://ift.tt/1IuPEkk;
<vareurl>http://ift.tt/1KwqUpp;
</produkt>
<produkter>


Any idea what I am doing wrong? How should improve my code so it finds all children?


No comments:

Post a Comment