Silddiombretma
New Member
I want to write a \[code\]Dictionary<int, List<Object>>\[/code\] data to XML file. I tried below code:\[code\]Dictionary<int, List<Author>> _empdata = http://stackoverflow.com/questions/14419991/new Dictionary<int, List<Author>>();List<Author> _author1 = new List<Author> { new Author() { id = 1, name ="Tom", age = 25 } };List<Author> _author2 = new List<Author> { new Author() { id = 2, name = "Jerry", age = 15 } };_empdata.Add(1, _author1);_empdata.Add(2, _author2);string fileName = "Author_" + string.Format("{0:yyyy-MM-dd_hh-mm-ss-tt}", DateTime.Now);string filePath = Path.Combine(Directory.GetCurrentDirectory(), fileName) + ".xml"; XElement elem = new XElement("StackOverflow"); foreach (KeyValuePair<int,List<Author>> pair in _empdata) { elem = new XElement("AUTHORDATA", from item in pair.Value select new XElement("AUTHOR", new XElement("ID", item.id), new XElement("NAME", item.name), new XElement("AGE", item.age) )); } elem.Save(filePath);\[/code\]My expected output is:\[code\]<AUTHORDATA> <AUTHOR> <ID>1</ID> <NAME>Tom</NAME> <AGE>25</AGE> </AUTHOR> <AUTHOR> <ID>2</ID> <NAME>Jerry</NAME> <AGE>15</AGE> </AUTHOR></AUTHORDATA>\[/code\]But,I am getting below records in XML file:\[code\] <AUTHORDATA> <AUTHOR> <ID>2</ID> <NAME>Jerry</NAME> <AGE>15</AGE> </AUTHOR> </AUTHORDATA>\[/code\]It is only writing the last List record in XML file every time. How can I fix this ? Note: The format of the XML file will be like above.Also, do I need to initialize \[code\]XElement\[/code\] in above code ?\[code\]XElement elem = new XElement("StackOverflow");\[/code\]Any help is appreciated.