0

I try to create an XML document from a recieved List<T> which T is the type of the list.

so, the problem is when I try to use a for loop inside XElement I get errors.

My idea is ccreating an XML Document contains elements based on the T properties

So please how can I use the for loop inside the XElement?

XDev
  • 125
  • 1
  • 8
  • 2
    If you get an error, please paste it into your question. That makes it easier for us to see the problem. However, you cannot have a for-loop within a call to a method - that's invalid syntax. You need to call the `Add` method inside the loop instead. – Xerillio Jan 13 '21 at 19:42
  • thats the errors https://imgur.com/MuiZ0pd – XDev Jan 13 '21 at 19:58

1 Answers1

1

As for your error, you cant pass a for-loop as a method parameter. I see what you're getting at here, but you're doing it kind of inverted. Instead of placing the for-loop into the add method, put the add method into the for loop:

for (int i = 0; i < PropertiesLength; i++)
{ 
    var element = new XElement(dataAsList_Properties[i].Name, dataAsList_Properties[i].GetValue(d));
    xdoc.Root.Element(childElementsName).Add(element);
}

Also:

Might I suggest having T implement a certain interface? This will allow you setup methods that are available across all of your types so that your data is more accessible for your conversion to XML

Chris Phillips
  • 1,997
  • 2
  • 19
  • 34