c# linq到xml错误:不能隐式转换类型

时间:2022-02-05 16:34:37

I'm getting the following error:

我得到以下错误:

Cannot implicitly convert type System.Collections.Generic.IEnumerable to System.Xml.Linq.XElement. An explicit conversion exists (are you missing a cast?)

不能隐式转换system . collection . generic类型。IEnumerable System.Xml.Linq.XElement。存在显式转换(您是否丢失了强制转换?)

I have an XML file that looks something like:

我有一个看起来像这样的XML文件:

<Users xmlns="http://schemas.microsoft.com/sharepoint/soap/directory/"> 
<User ID="91" Name="Jane Smith" LoginName="domain1\jsmith" />
<User ID="814" Name="Brad Jones" LoginName="domain1\bjones" />
<User ID="1252" Name="Charles Johnson" LoginName="domain2\cjohnson" />
</Users>

I want to select the node with a particular ID. Here's my code (the Where clause gives the error):

我想选择具有特定ID的节点,这是我的代码(Where子句给出错误):

XmlNode siteUsers = tempug.GetUserCollectionFromSite();
string innerXML = siteUsers.InnerXml;
XDocument siteUserData = XDocument.Parse(innerXML);
XElement userNode = siteUserData.Descendants("User")
  .Where(n => n.Attribute("ID").Value.Equals("12");

4 个解决方案

#1


2  

use

使用

XElement userNode = siteUserData.Descendants("User")
  .Where(n => n.Attribute("ID").Value.Equals("12").FirstOrDefault();

or

XElement userNode = siteUserData.Descendants("User")
  .Where(n => n.Attribute("ID").Value.Equals("12").SingleOrDefault();

you can use Single() and First() aswell, depending on how strict you want the return to be.

您也可以使用Single()和First(),这取决于您希望返回的严格程度。

#2


2  

You need to change .Where in your last line to .SingleOrDefault

您需要在最后一行中更改.SingleOrDefault

#3


2  

You need to tell linq you only want 1 node

您需要告诉linq您只想要一个节点

var userNode = siteUserData.Descendants("User")
  .FirstOrDefault(n => n.Attribute("ID").Value.Equals("12"));

#4


2  

.Where() will return an IEnumerable even if there is only one item in it. If you're expecting only one item to be returned you can call .First() or .FirstOrDefault() on the Enumerable.

. where()将返回一个IEnumerable,即使其中只有一个条目。如果只希望返回一个项,可以在Enumerable上调用. first()或. firstordefault()。

#1


2  

use

使用

XElement userNode = siteUserData.Descendants("User")
  .Where(n => n.Attribute("ID").Value.Equals("12").FirstOrDefault();

or

XElement userNode = siteUserData.Descendants("User")
  .Where(n => n.Attribute("ID").Value.Equals("12").SingleOrDefault();

you can use Single() and First() aswell, depending on how strict you want the return to be.

您也可以使用Single()和First(),这取决于您希望返回的严格程度。

#2


2  

You need to change .Where in your last line to .SingleOrDefault

您需要在最后一行中更改.SingleOrDefault

#3


2  

You need to tell linq you only want 1 node

您需要告诉linq您只想要一个节点

var userNode = siteUserData.Descendants("User")
  .FirstOrDefault(n => n.Attribute("ID").Value.Equals("12"));

#4


2  

.Where() will return an IEnumerable even if there is only one item in it. If you're expecting only one item to be returned you can call .First() or .FirstOrDefault() on the Enumerable.

. where()将返回一个IEnumerable,即使其中只有一个条目。如果只希望返回一个项,可以在Enumerable上调用. first()或. firstordefault()。