Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 行数作为属性值的Xattribute_C#_Xml_Linq_Linq To Xml - Fatal编程技术网

C# 行数作为属性值的Xattribute

C# 行数作为属性值的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.

借助list
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)));