Where子句在LINQ-To-XML中不起作用

本文关键字:不起作用 LINQ-To-XML 子句 Where | 更新日期: 2023-09-27 17:49:46

我的XML文件是

 <?xml version="1.0" encoding="utf-8"?>
 <root>
    <Message type="sms">
        <Details locale="en" message-type="User.ResetPassword" />
        <Context>
            <Parameter name="Time" value=" 16:03:31" />
            <Parameter name="pswr" value="00" />
            <Parameter name="Date" value="18/12/2014" />
        </Context>
        <Receiver>+923328749199</Receiver>
    </Message>
  </root>
我的c#代码是
XElement xelement = XElement.Load("C:''Users''qadeer.hussain''Desktop''gw-msg-2.xml");
var name = from nm in xelement.Elements("Message").Elements("Context").Elements("Parameter")
           where nm.Element("Receiver").Value == "+923328749199"
           select nm;

问题是我的where子句不工作,除非子句查询工作正常。你能帮我解决这个问题吗?

Where子句在LINQ-To-XML中不起作用

    XElement xelement = XElement.Load("C:''Users''qadeer.hussain''Desktop''gw-msg-2.xml");
    var name = from nm in xelement.Elements("Message")
               from mm in nm.Elements("Context").Elements("Parameter")  
               where nm.Element("Receiver").Value == "+923328749199"
               select mm;

<Receiver>不是<Parameter>的子元素,而是<Message>的子元素。试试这样:

XElement xelement = XElement.Parse(xml);
var name = from message in xelement.Elements("Message")
           from param in message.Elements("Context").Elements("Parameter")
           where (string)message.Element("Receiver") == "+923328749199"
           select param;