Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Hugs Are Drugs · Aug 14, 2013 at 07:03 PM · c#xmllinq

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

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 :/

Comment
Add comment · Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Hugs Are Drugs · Aug 14, 2013 at 10:20 PM 0
Share

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.

avatar image Hugs Are Drugs · Aug 14, 2013 at 10:32 PM 0
Share

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.

avatar image ArkaneX · Aug 14, 2013 at 11:14 PM 1
Share

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! :)

avatar image Hugs Are Drugs · Aug 14, 2013 at 11:38 PM 0
Share

err, I'm in C#, exactly what type of variable is goals in what you wrote?

avatar image ArkaneX · Aug 14, 2013 at 11:52 PM 1
Share

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.

Show more comments
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Dtd validation with TextAsset xml gives exception 0 Answers

C# Getting only the Name of the XML File and not the Directory of the XML File 2 Answers

Invalid Encoding Specification Xml 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges