简体   繁体   中英

How to convert List<string> to xml

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.

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))
                            )
                       );

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM