2017-04-25 34 views
1

我需要一组字符串转换为XML格式,我使用下面的代码列表转换成XML如何列出<string>转换为XML

XElement xmlElements = new XElement("DocumentElement", _UserIDs.Select(i => new XElement("BadgeNo", i))); 

当前结果:

<DocumentElement> 
<BadgeNo>IMS001</BadgeNo> 
<BadgeNo>IMS002</BadgeNo> 
<BadgeNo>IMS003</BadgeNo> 
<BadgeNo>IMS022</BadgeNo> 
<BadgeNo>WAN35166</BadgeNo> 
</DocumentElement> 

但是我需要更多的东西,我需要像这样添加一个额外的节点。我如何能实现以下输出

预期结果:在您的帮助

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

感谢。

回答

0

,同时通过新的 “BadgeNo” 元素作为参数选择新的 “GroupInput” 元素:

XElement xmlElements = new XElement("DocumentElement", 
          _UserIDs.Select(i => 
             new XElement("GroupInput", 
               new XElement("BadgeNo", i)) 
          ) 
         ); 
相关问题