我有一些丑陋的xml(如果我可以改变的话,我会改变的),我需要能够处理它们。我想按XML中的特定属性进行分组,以便进一步处理。问题是有些属性的type=是"A“,有些属性的type=是”A1“,type=是”A2“,等等。我希望所有属性类型值以”A“开头的元素都被分组在一起。
这不起作用,我不知道如何改变才能使其起作用:
var result = from ele in xdoc.Descendants(Namespace + "item")
let item= ele.Attribute("type").Value.Substring(1,1)
group ele by item into grp
select grp;当我去掉子字符串时,它工作得很好,但显然不能按我想要的方式进行分组。
错误:
Error has been thrown by the target of an invocation示例XML:
<items>
<item type="A" position="0">
<itemvalue>10</itemvalue>
</item>
<item type="A1" position="1">
<itemvalue>20</itemvalue>
</item>
<item type="A2" position="2">
<itemvalue>30</itemvalue>
</item>
<item type="B" position="0">
<itemvalue>10</itemvalue>
</item>
<item type="B1" position="1">
<itemvalue>20</itemvalue>
</item>
<item type="B2" position="2">
<itemvalue>30</itemvalue>
</item>
</items>发布于 2012-01-11 01:13:43
如果没有编译器在我的手机上输入,我会说你应该把0作为第一个参数传递给Substring,也就是Substring(0,1)。
https://stackoverflow.com/questions/8807152
复制相似问题