user3842125 user3842125 - 2 months ago 5
C# Question

XML variable not populated in c#

Here is a piece of code where i am trying to embed a list of integers into an XML.

function(long[] Idlist)
{
XDocument inputXML = new XDocument(
new XElement("Ids",
from wp in Idlist
select new XElement("element")));
}


I have FOUR values in my IdList[20,30,40,50] but still the input xml doesnt populate any values.

AND the inputXML is populated like this :

<Ids>
<element/>
<element/>
<element/>
<element/>
</Ids>


Any suggestions ?

Answer

Your query looks like:

from wp in Idlist
select new XElement("element")

You aren't using any data from your Idlist to feed into the XElement.

you've used this constructor which creates an empty element with the name you've provided.

Try using the correct constructor that allows you to pass in a value for the XElement as well as a name.