Linq to xml 和跨多个投影的计数器
与此问题类似:
我想使用投影中的计数器或递增变量 - 但是我想在多个投影中使用相同的计数器。我正在使用基于不同数据源的多个投影构建一个 xml 文档,但需要维护一个“id”节点(其中 id 是一个递增值)。
我可以使用类似于
Dim x as New XElement("rootNode", list.Select(Of XElement)(Function(xe, count) new XElement("blah", count+1)) 的 方法在一个投影中实现此目的)
然后,我想将另一组 XElement 添加到根,继续前一个值的计数器编辑
: 注意 - 上面的内容可能没有很好地描述我的问题 - 我想询问一个 xml 文档(由上面的列表表示)并基于一组节点,向另一个文档添加一些新节点。然后询问文档中的另一组节点,并向另一个文档添加更多新节点,同时维护两组节点的递增计数器。
即
Dim orig = <root>
<itemtype1>
<item>
<name>test</name>
<age>12</age>
</item>
<item>
<name>test2</name>
<age>13</age>
</item>
</itemtype1>
<itemtype2>
<item>
<name>testing</name>
<age>15</age>
</item>
<item>
<name>testing</name>
<age>14</age>
</item>
</itemtype2>
</root>
Dim newX As New XElement("test", orig.Descendants("itemtype1"). _
Descendants("item").Select(Of XElement)(Function(xe, count) New XElement("blah", New XElement("id", count))), _
orig.Descendants("itemtype2"). _
Descendants("item").Select(Of XElement)(Function(xe, count) New XElement("blah", New XElement("id", count))))
理想情况下这会输出:
<test>
<blah>
<id>0</id>
</blah>
<blah>
<id>1</id>
</blah>
<blah>
<id>2</id>
</blah>
<blah>
<id>3</id>
</blah>
</test>
Similar to this question:
LINQ, iterators, selecting and projection
I'm wanting to use a counter or incrementing variable in a projection - however I'm wanting to use the same counter across multiple projections. I'm constructing an xml document using a number of projections based on different data sources, but need to maintain an "id" node (where the id is an incrementing value).
I can achieve this for one projection using something similar to
Dim x as New XElement("rootNode", list.Select(Of XElement)(Function(xe, count) new XElement("blah", count+1)))
I then want to add another group of XElements to the root, continuing the counter from the previous value
Edit:
Note - the above perhaps doesn't describe my issue terribly well - I'm wanting to interrogate an xml document (which was represented by the list above) and based on one set of nodes, add some new nodes to another document. Then interrogate the document for another set of nodes, and add some more new nodes to the other document, maintaining the incrementing counter across both sets.
i.e.
Dim orig = <root>
<itemtype1>
<item>
<name>test</name>
<age>12</age>
</item>
<item>
<name>test2</name>
<age>13</age>
</item>
</itemtype1>
<itemtype2>
<item>
<name>testing</name>
<age>15</age>
</item>
<item>
<name>testing</name>
<age>14</age>
</item>
</itemtype2>
</root>
Dim newX As New XElement("test", orig.Descendants("itemtype1"). _
Descendants("item").Select(Of XElement)(Function(xe, count) New XElement("blah", New XElement("id", count))), _
orig.Descendants("itemtype2"). _
Descendants("item").Select(Of XElement)(Function(xe, count) New XElement("blah", New XElement("id", count))))
Ideally this would output:
<test>
<blah>
<id>0</id>
</blah>
<blah>
<id>1</id>
</blah>
<blah>
<id>2</id>
</blah>
<blah>
<id>3</id>
</blah>
</test>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最简单的可能是连接你的两个序列:(
格式化以提高可读性.)
The simplest is probably to concatenate your two sequences:
(Formatted for readability.)