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 MoyaAdam · Mar 10, 2014 at 08:14 AM · c#coroutinewww

XElement loop through elements in IEnumerator

I'm trying to download an XML file from a website, parse it into XElement and then read the information inside the document. I put it all in an IEnumerator and used the WWW method to get the information. However when I try to use a foreach the code reads no elements with the name "Name" however there are at least 10.

 IEnumerator GetFile() {
         print ("getting file");
         WWW www = new WWW(url);
         yield return www;
         print (www.text);
         XElement x = XElement.Parse(www.text);
         //x.Save(@"D:\doc.xml");
         foreach(XElement name in x.Elements("Name"))
         {
             print ("BOO");
         }
         print ("END");
     }

It gets the document, and if I uncomment the Save function it definitely saves the correct document, however the loop does not work. Here's an example of what the XML file looks (since you need an account to get it).

 <Data>
   <Results>
     <Item>
       <ID>3412</ID>
       <Name>The Second Sword of Tyr</Name>
       <Cost>5,000 gp</Cost>
       <Level>10</Level>
       <Rarity>Rare</Rarity>
       <Category>Weapon</Category>
       <SourceBook>Dungeon Magazine 196</SourceBook>
       <LevelSort>10</LevelSort>
       <CostSort>5000</CostSort>
     </Item>

Any help here?

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

1 Reply

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

Answer by wibble82 · Mar 10, 2014 at 09:01 AM

The 'Elements' function only looks at child elements, it doesn't search the whole file. So if you just loaded the doc you'd be looking at the 'Data' take I think (try debug logging out x.Name).

You could do that just with nested for loops, or use Linq queries with the 'SelectMany' operator. There may be a funky XElement function to do searches in child nodes, but I don't know of it :)

If you add the 'usings' for xml linq operations (I think System.Linq and System.Xml.Linq), your query would be something like:

 //linq query to extract all name elements
 IEnumerable<XElement> name_elements = x.Elements("Data")  //get all data elements
                                        .SelectMany(a = > a.Elements("Results")) //aggregate together all their 'Results' elements
                                        .SelectMany(a=> a.Elements("Item") //aggregate together all their 'Item' Elements
                                        .Where(a => a.Name=="Name") //extract elements called "Name"
 
 foreach(XElement e in name_elements)
 {
 //bla
 }
 

Note: that assumes you could have multiple data, results and item elements. Simpler if you don't, but I reckon you can work that one out.

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

21 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 avatar image avatar image avatar image avatar image

Related Questions

suspend a function time to recover a data C# 2 Answers

Coroutine needed here? 1 Answer

WWW, RESTful Service, and Threading 1 Answer

Yielding with WWW in Editor 9 Answers

Coroutines and Static function 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