noshitsherlock
noshitsherlock

Reputation: 1153

Using XDocument.Parse loads all into one element

I am consuming XML from a third party source but a can't seem to get the xml loaded correctly into a XDocument. It loads it all into one element, and sets the value of it to the rest of the xml.

The xml string looks like this (unformated, read from the responsestream).

<ArticleDetail xmlns="http://schemas.datacontract.org/2004/07/EEL.ArticleDatabase.WebService.Model" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><Approved>false</Approved><ArticleGroupCode>5304</ArticleGroupCode><ArticleGroupName>Standblenders</ArticleGroupName><ArticleNumber>052ASB2600</ArticleNumber><BrandLogoTypeResourceId/><BrandName>Electrolux</BrandName><Denomination>Köksmaskiner ASB2600 Electrolux</Denomination><Description>Exklusiv mixer i borstat stål som enkelt krossar is, 1,6l glaskanna OBS! Levereras som 2-pack</Description><Documents/><Ean>7319590015596</Ean><Id>1649151</Id><Images/><ModelNumber>ASB2600</ModelNumber><PackageQuantity>1</PackageQuantity><Parameters><Parameter><Category>Dimensioner</Category><DataType>long</DataType><Description/><DisplayName>Bredd</DisplayName><Name>Bredd</Name><Priority>1</Priority><Unit>mm</Unit><Value>290</Value></Parameter><Parameter><Category>Dimensioner</Category><DataType>long</DataType><Description/><DisplayName>Djup</DisplayName><Name>Djup</Name><Priority>2</Priority><Unit>mm</Unit><Value>240</Value></Parameter><Parameter><Category>Dimensioner</Category><DataType>long</DataType><Description/><DisplayName>Höjd</DisplayName><Name>Höjd</Name><Priority>3</Priority><Unit>mm</Unit><Value>380</Value></Parameter></Parameters><Published>false</Published><ReplacedByArticleNumber/><ReplacesArticleNumber/><SellText1>Kraftfull - perfekt för att krossa is.</SellText1><SellText2>Glaskanna som rymmer 1,6 liter, med 6-bladigt knivsystem.</SellText2><SellText3>Väldigt enkel rengöring av kanna och knivsystem</SellText3><SellText4/><SellText5/><StatusCode>0</StatusCode><SupplierArticleNumber>ASB2600</SupplierArticleNumber><SupplierCode>150</SupplierCode><SupplierName>Elon Elkedjan Logistic AB</SupplierName><Texts><TextItem><Text>Test</Text><Type>Short</Type></TextItem><TextItem><Text>Elon</Text><Type>Long</Type></TextItem></Texts></ArticleDetail>

And the rest of the code looks like this.

var xDoc = XDocument.Parse(xmlString);

For some reason everything is loaded into the one element and the value is set to.

false5304Standblenders052ASB2600ElectroluxKöksmaskiner ASB2600 ElectroluxExklusiv mixer i borstat stål som enkelt krossar is, 1,6l glaskanna OBS! Levereras som 2-pack73195900155961649151ASB26001DimensionerlongBreddBredd1mm290DimensionerlongDjupDjup2mm240DimensionerlongHöjdHöjd3mm380falseKraftfull - perfekt för att krossa is.Glaskanna som rymmer 1,6 liter, med 6-bladigt knivsystem.Väldigt enkel rengöring av kanna och knivsystem0ASB2600150Elon Elkedjan Logistic ABTestShortElonLong

Upvotes: 2

Views: 1562

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1062745

Cannot reproduce; it works fine. I think you are reading it wrong. For example, the following draws out the node names as a tree:

static void Main()
{
    string xmlString = @"<ArticleDetail xmlns=""http://schemas.datacontract.org/2004/07/EEL.ArticleDatabase.WebService.Model"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance""><Approved>false</Approved><ArticleGroupCode>5304</ArticleGroupCode><ArticleGroupName>Standblenders</ArticleGroupName><ArticleNumber>052ASB2600</ArticleNumber><BrandLogoTypeResourceId/><BrandName>Electrolux</BrandName><Denomination>Köksmaskiner ASB2600 Electrolux</Denomination><Description>Exklusiv mixer i borstat stål som enkelt krossar is, 1,6l glaskanna OBS! Levereras som 2-pack</Description><Documents/><Ean>7319590015596</Ean><Id>1649151</Id><Images/><ModelNumber>ASB2600</ModelNumber><PackageQuantity>1</PackageQuantity><Parameters><Parameter><Category>Dimensioner</Category><DataType>long</DataType><Description/><DisplayName>Bredd</DisplayName><Name>Bredd</Name><Priority>1</Priority><Unit>mm</Unit><Value>290</Value></Parameter><Parameter><Category>Dimensioner</Category><DataType>long</DataType><Description/><DisplayName>Djup</DisplayName><Name>Djup</Name><Priority>2</Priority><Unit>mm</Unit><Value>240</Value></Parameter><Parameter><Category>Dimensioner</Category><DataType>long</DataType><Description/><DisplayName>Höjd</DisplayName><Name>Höjd</Name><Priority>3</Priority><Unit>mm</Unit><Value>380</Value></Parameter></Parameters><Published>false</Published><ReplacedByArticleNumber/><ReplacesArticleNumber/><SellText1>Kraftfull - perfekt för att krossa is.</SellText1><SellText2>Glaskanna som rymmer 1,6 liter, med 6-bladigt knivsystem.</SellText2><SellText3>Väldigt enkel rengöring av kanna och knivsystem</SellText3><SellText4/><SellText5/><StatusCode>0</StatusCode><SupplierArticleNumber>ASB2600</SupplierArticleNumber><SupplierCode>150</SupplierCode><SupplierName>Elon Elkedjan Logistic AB</SupplierName><Texts><TextItem><Text>Test</Text><Type>Short</Type></TextItem><TextItem><Text>Elon</Text><Type>Long</Type></TextItem></Texts></ArticleDetail>";
    var xDoc = XDocument.Parse(xmlString);
    Write(xDoc.Root, 0);
}

static void Write(XElement el, int offset)
{
    Console.Write(new string(' ', offset));
    Console.WriteLine(el.Name.LocalName);
    foreach (var child in el.Elements())
    {
        Write(child, offset + 1);
    }
}

I suspect you are accessing xDoc.Root.Value - indeed, what you display is the composed value of the root; but that doesn't mean the structure is missing. If that isn't what you wanted, then don't ask if for that value - walk the structure instead.

Upvotes: 4

Related Questions