linqToXml 中的条件

发布于 2024-10-24 18:21:45 字数 820 浏览 5 评论 0原文

XML

<Questions>
   <Question>
      <Id>1</Id>
      <Text>aaa</Text>
    </Question>
    <Question>
      <Id>2</Id>
      <Text>bbb</Text>
   </Question>
</Questions>

代码

question="aaa";

var doc = XDocument.Load(Server.MapPath(".") + "\\Questions.xml");
        var elements = from element in doc.Descendants("Question")
                       let txt = element.Element("Text")
                       where question.CompareTo (txt)==0 
                       select new
                       {
                           Id = element.Element("Id").Value,
                       };

此代码 elements.count()==>0

我希望从 xml 中进行选择,其中 txt=='aaa'

XML

<Questions>
   <Question>
      <Id>1</Id>
      <Text>aaa</Text>
    </Question>
    <Question>
      <Id>2</Id>
      <Text>bbb</Text>
   </Question>
</Questions>

Code

question="aaa";

var doc = XDocument.Load(Server.MapPath(".") + "\\Questions.xml");
        var elements = from element in doc.Descendants("Question")
                       let txt = element.Element("Text")
                       where question.CompareTo (txt)==0 
                       select new
                       {
                           Id = element.Element("Id").Value,
                       };

This code elements.count()==>0

I would Like that select from xml where txt=='aaa'

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

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

发布评论

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

评论(1

流星番茄 2024-10-31 18:21:45

let txt = element.Element("Text") 行返回 XElement 而不是文本,因此您的 CompareTo 条件将失效,而不是检查文本值。

相反,您可以通过 .Value 属性获取节点的值。

...
let txt = element.Element("Text").Value
...

var elements = from element in doc.Descendants("Question") 行将成功查找元素,但作为实践,您可能希望从根节点或相对层次结构查找。

...
var elements = from element in doc.Root.Descendants("Question")
...

代码的其余部分看起来很好(较少的异常处理)。

以下内容对我有用......

string xml = @"<Questions>
   <Question>
      <Id>1</Id>
      <Text>aaa</Text>
    </Question>
    <Question>
      <Id>2</Id>
      <Text>bbb</Text>
   </Question>
</Questions>";

string question = @"aaa";
var doc = XDocument.Parse(xml);
var elements = from element in doc.Root.Descendants("Question")
           let txt = element.Element("Text").Value
           where question.CompareTo(txt)==0 
           select new
           {
               Id = element.Element("Id").Value,
           };

Console.WriteLine(elements.Count()); //1

The line let txt = element.Element("Text") returns a XElement instead of the text so your CompareTo condition will blow up instead of checking the text values.

Instead you can get the value of the node via the .Value property.

...
let txt = element.Element("Text").Value
...

The line var elements = from element in doc.Descendants("Question") will successfully lookup the elements but as a practice you might want to go from the root node or relative hierarchy.

...
var elements = from element in doc.Root.Descendants("Question")
...

The rest of the code seems fine (less exception handling).

The following worked for me...

string xml = @"<Questions>
   <Question>
      <Id>1</Id>
      <Text>aaa</Text>
    </Question>
    <Question>
      <Id>2</Id>
      <Text>bbb</Text>
   </Question>
</Questions>";

string question = @"aaa";
var doc = XDocument.Parse(xml);
var elements = from element in doc.Root.Descendants("Question")
           let txt = element.Element("Text").Value
           where question.CompareTo(txt)==0 
           select new
           {
               Id = element.Element("Id").Value,
           };

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