Pablo Costa

Pablo Costa

  • NA
  • 24
  • 4.8k

List<List<T>> to XML with LINQ

Feb 25 2016 11:54 PM

0down votefavorite

I've changed my approach and decided to start writing XML with Linq. I've readed some tutorials and was able to write with no problem.

However , i don't know how to write elements iterating over my array of lists.

I've a nested foreach:

 foreach(List<xmldata> i in XMLList.XMLArrayList) { 
foreach (var x in i) { } }

that i can use to access elements of my list. Since its an array of list ,

each position of the array is going to have a list with three elements from this class:
 public class xmldata //Class to receive items list
 { public string xml_filename { get; set; } 
public string colorname { get; set; } 
public string colorvalues { get; set; } }

However, if i directly write x.colorname into the Linq as an XAttribute , in the next

iteration it'll overwrite its value and in the end the fill will have only the last element.

i.e:

List[0][1]
 colorname blue 
colorvalue #434344
 xml_filename filename 
List[0][2] 
colorname cyan 
colorvalue #414354
 xml_filename filename 
List[0][3]
 colorname magenta 
colorvalue #43213
 xml_filename filename

When the foreach loop finishes iterating over this position, the XML File is going to have only the magenta written. And i would like to have all values written (since it'll iterate over all list positions).

How can i achieve that ?

Thanks in advance.