简体   繁体   English

我怎样才能从LINQ中对XAttribute值进行子串

[英]How I can I Substring XAttribute Value from LINQ

I have some ugly xml(if I could change I would) coming to me that I need to be able to process. 我有一些丑陋的xml(如果我可以改变的话)来找我,我需要能够处理。 I wanted to group by a specific attribute in the XML for further processing. 我希望按XML中的特定属性进行分组以进行进一步处理。 The problem is that some of the attributes have a type="A" and some have type="A1", type="A2", etc. I want all of the elements that have an attribute type value that starts with "A" be grouped together. 问题是某些属性的类型为“A”,有些属性为type =“A1”,类型=“A2”等。我希望所有属性类型值以“A”开头的元素被组合在一起。

This is not working and I don't know what to change to get it to work: 这不起作用,我不知道要改变什么来让它工作:

    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;

When I take off the substring it works fine, but obviously doesn't group how I want. 当我取下子串时,它工作正常,但显然不能将我想要的分组。

Error: 错误:

Error has been thrown by the target of an invocation

Example XML: 示例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>

如果没有我自己编写的编译器并在我的手机上输入,我会说你应该将0作为第一个参数传递给Substring ,即Substring(0,1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM