- Home /
LINQ, XML, putting XML nodes into an array
I'm learning to access XML files in C#, which to me is definently out of my knowledge and I'm having a hard time grasping, so please be simple and explain things well.
My question is pretty simple, how can I add a group of XML files into a list? I'm using LINQ and I'm attempting to assign a list with this code.
XMLfile.Element ("data").Element ("level").Elements ("goal").Select (c => (string)c).ToList(array);
But there's no definition for .select, "Type Systems.Collections.Generic.IEnumberable does not contain a definition for Select and no extension method Select of type System.Collections.Generic.IEnumerzecan be found (are you missing a using directive or an assembly reference?)"
wat is the issue, i know too little.
Also ehat does c=> (string)c mean, ad what us it's use in this?
Answer by ArkaneX · Aug 14, 2013 at 08:57 PM
Firstly, let's analyze the code you wrote. Assuming you have a variable named XMLfile (most likely of type XDocument), you're:
selecting from this file root element 'data'
selecting 'level' element under 'data'
selecting list of 'goal' elements under 'level'
casting each 'goal' element to string and returning
IEnumerable
creating List from above
IEnumerable
(btw - array parameter is not necessary there and will result in compile error)
At the end you will then have a List of XElements cast to string, and I guess this is not what you wanted. If you want to have a list of XML files, then please write a bit more about your way of reading the files, and, most importantly, what do you want to accomplish.
You get error related to Select, because in order to use Linq extension methods like Select(), OrderBy(), Where() etc. you have to include System.Linq namespace in your code:
using System.Linq;
After this, a lot of new methods will be available for arrays, list, collections and other types implementing IEnumerable interface.
And lastly, expression having syntax:
(input parameters) => expression
is lambda expression, and basically it means that for given parameters some specific expression will be executed. This is VERy simple definition, and I strongly suggest reading through MSDN pages linked above.
EDIT: in the bulleted list on the top I wanted to write
IEnumerable<string>
twice, but unfortunately I have yet to discover how to use lt; & gt; in normal text :/
I just want to take a bunch of variables from an X$$anonymous$$L file and store them in a public, static array and then access the information from other scripts that need the info. Is this not what this line will do?
I have 4 elements with the name "goal" and with IDs that go 1 to 4.
I also have another question, how do I access an element with a name and in ID, if I had an element named level I'd = "1", what do I type to specify the ID, someone told me it's something lik Element ("level")["1"], but that doesn't help.
But thanks, that worked, I already had System.Xml.Linq, I thought that'd be the issue.
okay, so I did another copy paste job and got this, which I believe is what I want? (I will learn, I swear) X$$anonymous$$Lfile.Element ("data").Element ("level").Element ("goals").Elements ().Select (c => array.Add (System.Convert.ToString (c.Value)));
but then I get the error
The type of arguments for method 'System.Linq.Enumerable.Select<TSource.TResult>(System.Collections.Generic.IEnumberable<TSource, TResult>)' cannot be inferred from the usage. try specifying the type Augments explicitly.
maybe i should do some research and practice before I try to do things like this.
Ah - so you need to store values, not X$$anonymous$$L files. Now it makes more sense :)
Let's assume, your xml looks like this:
<data>
<level>
<goal ID='1'>data1</goal>
<goal ID='2'>data2</goal>
<goal IDx=''>data3</goal>
<goal ID='4'>data4</goal>
</level>
</data>
To select the goal values only, you have to use:
var goals = doc.Element("data").Element("level").Elements("goal");
var array = goals.Select(x => x.Value).ToArray();
The result is string[] containing four values (data1, data2, data3, data4).
To select data by ID, use:
var goalsWithId = goals.Where(x => x.Attribute("ID") != null);
var third = goalsWithId.FirstOrDefault(x => x.Attribute("ID").Value == "3");
First line selects only those 'goal' elements, that have 'ID' attribute, the second one selects element with ID equal to 3. If there were no element with such ID, 'third' variable would be null.
Ins$$anonymous$$d of loading the element values to array, you can load them to Dictionary as well:
Dictionary<int, string> dic = goalsWithId.ToDictionary(x => Int32.Parse(x.Attribute("ID").Value), x => x.Value);
This way, you can access values using dic[1], dic[2], etc.
And remember - you swore to learn! :)
err, I'm in C#, exactly what type of variable is goals in what you wrote?
var is C# keyword as well and it can be used ins$$anonymous$$d of any other type name. In contrary to object variables or dynamic variables, instances declared as var are strongly typed.
In the above example, goals and goalsWithId are
IEnumerable<XElement>
array is string[], and third is XElement.
Answer by jeryymanly · Nov 12, 2016 at 07:40 AM
Try this one...[read xml][1]
Jerry [1]: http://csharp.net-informations.com/xml/how-to-read-xml.htm