使用子属性查询子元素的 XElement

发布于 2024-08-31 19:21:51 字数 1117 浏览 4 评论 0原文

以下是 XML 概要:

<Root> 
  <Thing att="11">    
    <Child lang="e">  
       <record></record>
       <record></record>
       <record></record>  
   </Child >
   <Child lang="f">  
       <record></record>  
       <record></record>                
       <record></record>
   </Child > 
 </Thing> 
</Root>

我有以下内容:

TextReader reader = new StreamReader(Assembly.GetExecutingAssembly()
                 .GetManifestResourceStream(FileName));

   var data = XElement.Load(reader);
foreach (XElement single in Data.Elements())
 {
      // english records
      var EnglishSet = (from e in single.Elements("Child")
         where e.Attribute("lang").Equals("e")
        select e.Value).FirstOrDefault();
}

但我什么也没得到。我希望能够为每个“事物”选择“子”,其中属性“lang”等于一个值。

我也尝试过这个,但没有成功。

var FrenchSet = single.Elements("Child")
.Where(y => y.Attribute("lang").Equals("f"))
.Select(x => x.Value).FirstOrDefault();

Here is the XML outline:

<Root> 
  <Thing att="11">    
    <Child lang="e">  
       <record></record>
       <record></record>
       <record></record>  
   </Child >
   <Child lang="f">  
       <record></record>  
       <record></record>                
       <record></record>
   </Child > 
 </Thing> 
</Root>

I have the following:

TextReader reader = new StreamReader(Assembly.GetExecutingAssembly()
                 .GetManifestResourceStream(FileName));

   var data = XElement.Load(reader);
foreach (XElement single in Data.Elements())
 {
      // english records
      var EnglishSet = (from e in single.Elements("Child")
         where e.Attribute("lang").Equals("e")
        select e.Value).FirstOrDefault();
}

But I'm getting back nothing. I want to be able to for Each "Thing" select the "Child" where the attribute "lang" equals a value.

I have also tried this, which has not worked.

var FrenchSet = single.Elements("Child")
.Where(y => y.Attribute("lang").Equals("f"))
.Select(x => x.Value).FirstOrDefault();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

拥醉 2024-09-07 19:21:51

您正在检查 XAttribute 对象是否等于字符串 "e"
由于 XAttribute 对象永远不等于字符串,因此它不起作用。

您需要检查 XAttribute 对象的 Value,如下所示:

where y => y.Attribute("lang").Value == "e"

You're checking whether the XAttribute object is equal to the string "e".
Since an XAttribute object is never equal to a string, it doesn't work.

You need to check the XAttribute object's Value, like this:

where y => y.Attribute("lang").Value == "e"
小清晰的声音 2024-09-07 19:21:51

您正在将属性对象与字符串“e”进行比较,而不是与属性对象的值进行比较。您还返回节点的值而不是节点本身。由于该值为空,因此您只会得到空字符串。

试试这个:

var EnglishSet = (from e in single.Elements("Child")
                  where e.Attribute("lang").Value == "e"
                  select e).FirstOrDefault();

You are comparing the attribute object with the string "e", rather than the value of the attrbute object. You're also returning the value of the node rather than the node. Since the value is empty, you'll just get the empty string.

Try this instead:

var EnglishSet = (from e in single.Elements("Child")
                  where e.Attribute("lang").Value == "e"
                  select e).FirstOrDefault();
羁绊已千年 2024-09-07 19:21:51
var EnglishSet = (from e in single.Elements("Child")
         where e.Attribute("lang").Value.Equals("e")
        select e).FirstOrDefault();

正如 Slaks 所说,您正在检查属性而不是其值是“e”。您也不需要 select e.Value 因为“Child”节点没有值,它们有“record”子节点。

var EnglishSet = (from e in single.Elements("Child")
         where e.Attribute("lang").Value.Equals("e")
        select e).FirstOrDefault();

As Slaks stated you were checking that the attribute not it's value was "e". You also don't need select e.Value because the "Child" nodes don't have a value they have "record" children.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文