如何将List <int>项写入xml文件?

Alf*_*igo 0 c# xml linq xelement list

我需要将项目写入List<int> myListxml文件.xml文件的外观应该是列表中的值是1,2和3.我知道这可以使用linq.我想避免序列化程序选项.

<List>
  <itemValue>1</itemValue>
  <itemValue>2</itemValue>
  <itemValue>3</itemValue>
</List>
Run Code Online (Sandbox Code Playgroud)

小智 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");
}
Run Code Online (Sandbox Code Playgroud)