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 20, 2015 at 01:49 PM · c#xmlparsing

C# XML Question

Currently I am trying to read this XML file

 <?xml version=”1.0" encoding=”utf-8" ?>
 
 <KenoNumbers>
     <numbers num="1" />
     <numbers num="4" />
     <numbers num="32" />
     <numbers num="54" />
 </KenoNumbers>

Now I am fairly new to C# and using it to parse and read an XML file. Currently I am not to sure on if what I have so far actually is parsing the data and grabbing the numbers, because I am running into an issue of SerializerObj does not exist in the current state and this is also making the error of Deserialize, that error is just a ?. Below is where and I'm trying to read and store the data, but currently I'm not getting the code to run nor am I really sure if I'm actually parsing and grabbing the numbers from the XML.

     public class GameManager : MonoBehaviour
     {
         [XmlRoot("kenoInfo")]
         public class kenoInfo{
             [XmlElementAttribute("num")]   
             public string nums;   
         }
         
         void getXml () {
             // Create a new file stream for reading the XML file
             FileStream ReadFileStream = new FileStream(@"C:\Users\Kyle\Downloads\yj\Assets\Script\keno.xml", FileMode.Open, FileAccess.Read, FileShare.Read);
             
             // Load the object saved above by using the Deserialize function
             kenoInfo LoadedObj = (kenoInfo)/*issue*/SerializerObj.Deserialize(ReadFileStream); 
             
             // Cleanup 
             ReadFileStream.Close();
             
             // Test the new loaded object:
             Debug.Log(LoadedObj.nums);
         } 
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
0
Best Answer

Answer by WillNode · May 20, 2015 at 02:45 PM

you can do all XML Related files better with XMLDocument class.

 using System.Xml // Always import this reference
 public class SomeClass : MonoBehavior {
  void Start(){
   XmlDocument x =     new XmlDocument ();
   x.Load("somePath");  // Load by Path
   x.LoadXml ("someXMLString"); // Load by XML String data
 
   XmlNodeList nodeList;
   XmlNode root = x.DocumentElement;
         
   nodeList = root.SelectNodes("/Node/ChildNode"); // Find Child by XPath Expression
         
   foreach (XmlNode node in nodeList){
     Debug.Log(node);
   }
  }
 }

see XPath examples here

Comment
Add comment · Show 2 · 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 20, 2015 at 04:00 PM 0
Share
 newXml.Load (@"C:\Users\$$anonymous$$yle\Downloads\yj\Assets\Script\keno.xml");  // Load by Path 

seems to be giving me the error XmlException: invalid encoding specification. System.Xml.XmlInputStream.Initialize (System.IO.Stream stream)

any idea why?

avatar image cjdev · May 20, 2015 at 06:44 PM 0
Share

You have the wrong kind of quotation marks in the top of your xml file.

Also, try using relative file paths like so:

 x.Load(Application.dataPath + "/Script/keno.xml");

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

20 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

Related Questions

Parsing an XML file periodically 2 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

I have some issues with implementing polar coordinates written in an Xml File into unity and visualise them afterwards. 0 Answers

XML parsing c# in unity 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