How the below content can be convert to xml using LINQ
List<int> calllist = new List<int>();
calllist.Add(10);
calllist.Add(5);
calllist.Add(1);
calllist.Add(20);
The output should be:
<root>
<child>
<name>1</name>
<count>1</count>
</child>
<child>
<name>5</name>
<count>1</count>
</child>
<child>
<name>10</name>
<count>1</count>
</child>
<child>
<name>20</name>
<count>1</count>
</child>
</root>
I tried something like:
XElement root = new XElement ("root",
new XElement("child",new XElement(from c in calllist select c; /*error here*/ )));
But got stuck up and unable to proceed. Can anyone share a solution to make this work?
@user833985
Try the below.
XElement root = new XElement(
"root", from c in calllist orderby c select
new XElement("child",
new XElement("name", c),
new XElement("count",calllist.Count))
);
XElement root =
new XElement("root",
calllist
.GroupBy(c => c)
.OrderBy(g => g.Key)
.Select(g => new XElement("child",
new XElement("name", g.Key),
new XElement("count",g.Count())
)
)
);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.