0

I need to write items in the List<int> myList into an xml file. The way the xml file should look like is if the values in the list are 1, 2 and 3. I know this is possible using linq. I want to avoid the serializer options.

<List>
  <itemValue>1</itemValue>
  <itemValue>2</itemValue>
  <itemValue>3</itemValue>
</List>
Alfred Waligo
  • 2,809
  • 3
  • 18
  • 26
  • You can use the [XmlSerializerClass](https://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer%28v=vs.110%29.aspx) class. Read [here](http://stackoverflow.com/questions/2292480/xmlserializer-list-item-element-name), it is a similar question. – Mehrzad Chehraz Apr 26 '15 at 17:52

1 Answers1

5
private static void ToXml(List<int> list)
{
    var doc = new XDocument(new XElement("List", list.Select(x =>
                          new XElement("itemValue", x))));
    doc.Save("test.xml");
}
Hossein Narimani Rad
  • 31,361
  • 18
  • 86
  • 116
akekir
  • 523
  • 4
  • 9