Reputation: 464
I need to convert a List of String to XML format, I am using the below code to convert the List to XML
XElement xmlElements = new XElement("DocumentElement", _UserIDs.Select(i => new XElement("BadgeNo", i)));
Current Result:
<DocumentElement>
<BadgeNo>IMS001</BadgeNo>
<BadgeNo>IMS002</BadgeNo>
<BadgeNo>IMS003</BadgeNo>
<BadgeNo>IMS022</BadgeNo>
<BadgeNo>WAN35166</BadgeNo>
</DocumentElement>
But I need something more, I need to add an extra node in like this. How can I achieve the below output
Expected Result:
<DocumentElement>
<GroupInput>
<BadgeNo>IMS001</BadgeNo>
</GroupInput>
<GroupInput>
<BadgeNo>IMS002</BadgeNo>
</GroupInput>
<GroupInput>
<BadgeNo>IMS003</BadgeNo>
</GroupInput>
<GroupInput>
<BadgeNo>IMS022</BadgeNo>
</GroupInput>
<GroupInput>
<BadgeNo>WAN35166</BadgeNo>
</GroupInput>
</DocumentElement>
Thanks in Advance for your help.
Upvotes: 1
Views: 1191
Reputation: 89285
Select new "GroupInput" element while passing new "BadgeNo" element as the parameter :
XElement xmlElements = new XElement("DocumentElement",
_UserIDs.Select(i =>
new XElement("GroupInput",
new XElement("BadgeNo", i))
)
);
Upvotes: 1