【发布时间】:2013-03-13 16:00:55
【问题描述】:
我有一个用户提供的数据表,其中包含以下信息:
| Name | Alias |
|---------------------------
| Batman | Dark Knight |
| Superman | Man of Steel |
| Spiderman | null |
我需要将此表转换为 Xml 以发送到存储过程并获取类似于以下内容的 Xml:
<Superheroes>
<Superhero Name="Batman" Alias="Dark Knight"/>
<Superhero Name="Superman" Alias="Man of Steel"/>
<Superhero Name="Spiderman"/>
</Superheroes>
我想使用 Linq-to-Xml(用于学习目的)来解决这个问题。到目前为止,这是我所拥有的:
XDocument doc;
doc = new XDocument(
new XElement("Superheroes",
from row in table.AsEnumerable()
select new XElement("Superhero",
new XAttribute("Name", row.Field<string>("Name")),
new XAttribute("Alias", row.Field<string>("Alias"))
)));
问题出在蜘蛛侠那一排。上面的查询因为 null 而不起作用。
问题
如何处理 Linq-to-Xml 中的空蜘蛛侠行?
如果用户忘记添加别名行,我需要能够处理该位置。在这种情况下,别名应该是 (a) 根本不在 Xml 中或 (b) 具有空值
【问题讨论】:
标签: c# .net linq linq-to-xml