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 Xzapo · May 21, 2015 at 01:31 PM · c#xmldataread

Reading XML Data C#

Hey guys, I'm currently having trouble on trying to read the data from my XML doc. I am fairly new to C# and it's causing some issues for me. I've been looking around and I just can't seem to figure out what I'm doing wrong or whats the right way of doing it since I've found like 100 different ways!

Here's the XML I am trying to read.

 <KenoNumbers>
     <numbers num="1" />
     <numbers num="4" />
     <numbers num="32" />
     <numbers num="54" />
 </KenoNumbers>

Here's where I'm trying to read the XML.

 XmlDocument newXml = new XmlDocument ();
             newXml.Load(Application.dataPath + "/Script/keno.xml"); 
     
             XmlNode root = newXml.DocumentElement;
             
             nodeList = root.SelectNodes ("(KenoNumbers/numbers)[1]"); 
     
             print(nodeList); 

If someone could help lead me in the right direction, or let me know if I'm close. Currently nodeList is just showing (Object) in debug.

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
0

Answer by spiceboy9994 · May 21, 2015 at 02:01 PM

The reason for that it is because the result for that operation is a XMLNodeList. Check this out

https://msdn.microsoft.com/en-us/library/hcebdtae(v=vs.110).aspx

If you want to get the value of the nodes of the returned list you could try

 foreach (XmlNode xn in nodeList)
 {
   string value1 = xn["MyFirstValue"].InnerText;
   string value2 = xn["MySecondValue"].InnerText;
   print("Node Values: {0} {1}", value1 , value2 );
 }


BTW, I think your XPath for selecting the nodes is not correct. Get of rid of the parenthesis within the path. Here's a link for XPath syntax

http://www.w3schools.com/xpath/xpath_syntax.asp

Regards

Comment
Add comment · Show 3 · 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 Xzapo · May 21, 2015 at 02:25 PM 0
Share

I just changed up how I'm grabbing the nodes, but in my print statement it keeps saying object. This is what I just tried... XmlDocument newXml = new XmlDocument (); newXml.Load(Application.dataPath + "/Script/keno.xml");

         XmlDocument xmlDoc = new XmlDocument();
     
         numList = xmlDoc.GetElementsByTagName("$$anonymous$$enoNumbers");
         
         print(numList); 
avatar image Landern · May 21, 2015 at 02:30 PM 1
Share

@Xzapo, you just ended up with a NodeList that should have for elements in it, each node would be called numbers, each numbers element would have an attribute called num with a string value of whatever the heck is in it.

If you use what you've already done, you need to iterate over the collection in the NodeList object and grab your values. @spiceboy9994 already lined the $$anonymous$$SDN article that shows as much.

avatar image spiceboy9994 · May 21, 2015 at 02:32 PM 0
Share

That method also returns a nodelist

https://msdn.microsoft.com/en-us/library/dc0c9ekk(v=vs.110).aspx

Just add the foreach as I mentioned before... and replace the strings for your node inner nodes. Or if you do not have inner nodes and just want to print the first node value, try this:

 var firstNode = numList[0];
 print(firstNode.Value);
avatar image
0

Answer by vks123 · Sep 11, 2020 at 09:18 AM

It is an old answer, but I will suggest to use Linq when parsng XML, here is an example, Where books are fetched

      var NodeStr = "";
         //get all elements inside book
         foreach (XElement level1Element in XElement.Load(mainpath).Elements("book"))
         {               
             //print each element value
             //you can also print XML attribute value, instead of .Element use .Attribute
                 NodeStr = NodeStr + "\nAuthor " + level1Element.Element("author").Value;
                 NodeStr = NodeStr + "\nTitle " + level1Element.Element("title").Value;
                 NodeStr = NodeStr + "\nGenre " + level1Element.Element("genre").Value;
                 NodeStr = NodeStr + "\nPrice " + level1Element.Element("price").Value;
                 NodeStr = NodeStr + "\nDescription -" + level1Element.Element("description").Value;                         
         }

Source: Open and Read XML in C# (Examples using Linq, XMLReader, XMLDocument)

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

Xml Serialization? (how to read/write?) 1 Answer

Unity C# Post XML to endpoint 0 Answers

XML Data binding in unity 5 using c# 0 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers


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