C # linq to xml to list

I was wondering if there is a way to get a list of results into a list with linq for xml. If I had the following xml, for example:

<?xml version="1.0"?> <Sports xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <SportPages> <SportPage type="test"> <LinkPage> <IDList> <string>1</string> <string>2</string> </IDList> </LinkPage> </SportPage> </SportPages> </Sports> 

How can I get a list of strings from an IDList?

I am new to linq for xml, so I just tried something, I am here now:

 var IDs = from sportpage in xDoc.Descendants("SportPages").Descendants("SportPage") where sportpage.Attribute("type").Value == "Karate" select new { ID = sportpage.Element("LinkPage").Element("IDList").Elements("string") }; 

But the var should randomly read decently. Isn't there a way to get a list of strings from this?

thanks

+4
source share
5 answers

This request works - verified and verified:

 var ID2 = (from sportpage in xDoc.Descendants("SportPages").Descendants("SportPage") where sportpage.Attribute("type").Value == "Karate" select sportpage) .Descendants("LinkPage") .Descendants("IDList") .Elements("string") .Select(d => d.Value) .ToList(); 

Gives me a list of two lines: "1" and "2".

+6
source
 var myStrings = xDoc.Descendants("SportPage") .Where(d => d.Attribute("type").Value == "Karate") .Descendants("IDList") .Descendants("string") .Select(d => d.Value); 

to see your line:

 xDoc.Descendants("SportPage") .Descendants("IDList") .Where(d => d.Attribute("type").Value == "Karate") .Descendants("string") .Select(d => d.Value) .ToList() .ForEach(Console.WriteLine); 
+2
source

Do you mean this?

 List<string> IDs = xDoc.Descendants("SportPages").Descendants("SportPage") .Where( anySportPage => anySportpage.Attribute("type").Value == "Karate" ) .Select( karateSportPage => karateSportpage.Element("LinkPage").Element("IDList").Elements("string")) .ToList(); 
+1
source

I think the reason you discover the chaos of "var" is to create an anonymous type with a "new" in your choice. If you just select the one item you need, then var will not be anonymous.

eg.

 select sportpage.Element("LinkPage").Element("IDList").Elements("string"); 

However, my preference would be to do this with help. designation like this.

 List<string> ids = xDoc.Elements("SportPages").Elements("SportPage").Where(sportPage => sportPage.Attribute("type").Value == "Karate").Elements("LinkPage").Elements("IDList").Elements("string").Select(id => id.Value).ToList(); 
0
source

The biggest problem you encountered was that you did not .Value from the set of returned elements. But here is another way to do this.

 var ids = from sportPage in xDoc.Descendants("SportPage") let attrib = sportPage.Attribute("type") where attrib != null let type = attrib.Value where !string.IsNullOrEmpty(type) && type == "Karate" from id in sportPage.Descendants("IDList").Elements() select id.Value; 
0
source

Source: https://habr.com/ru/post/1306363/


All Articles