C# 行数作为属性值的Xattribute
借助listC# 行数作为属性值的Xattribute,c#,xml,linq,linq-to-xml,C#,Xml,Linq,Linq To Xml,借助listseriesDetails,使用LINQ Im生成示例XML var result= from sc in seriesDetails select new XElement("A", new XAttribute("id", sc.A), new XElement("B",sc.B), new XAttribute("id", sc.
seriesDetails
,使用LINQ Im生成示例XML
var result=
from sc in seriesDetails
select new XElement("A",
new XAttribute("id", sc.A),
new XElement("B",sc.B),
new XAttribute("id", sc.B));
生成如下所述的XML示例
<A id="asdf">
<B id="qwer" />
</A>
<A id="sdfg">
<B id="bmnm" />
</A >
感谢任何帮助:)您可以使用提供行号(即元素索引)的方法:
另一个选项是在查询外部引入新变量,该变量将保留行数
int sequence = 0;
var result=
from sc in seriesDetails
select new XElement("A",
new XAttribute("id", sc.A),
new XAttribute("sequence", ++sequence),
new XElement("B",
new XAttribute("id", sc.B)));
谢尔盖·别列佐夫斯基-谢谢!关于如何为子元素
添加序列号的另一个问题?@user2729272是否要为属性B添加相同的序列号?是的,在
下为每个
添加序列号。类似于,
@user2729272您可以使用与第一个选项相同的方法-在选择b项时使用Select((b,i)=>new{b,sequence=i+1}
from x in seriesDetails.Select((sc, i) => new { sc, sequence = i + 1 })
select new XElement("A",
new XAttribute("id", x.sc.A),
new XAttribute("sequence", x.sequence),
new XElement("B", // do not provide inner text to element B
new XAttribute("id", x.sc.B)));
int sequence = 0;
var result=
from sc in seriesDetails
select new XElement("A",
new XAttribute("id", sc.A),
new XAttribute("sequence", ++sequence),
new XElement("B",
new XAttribute("id", sc.B)));