Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Majk- · Aug 15, 2017 at 12:30 PM · script.xmlreading data

Proper way to read an XML document

I have an XML file:

For some reason the code is all messed up even though it shows up right in the preview... Link: https://gyazo.com/d9a050f35a553dfe7718a7323e01bdb0

<u1 id="1"> <name>Damage over time</name> <desc>This is an upgrade Damage over time</desc> </u1> <u2 id="2"> <name>Double Damage</name> <desc>This is an upgrade Double damage</desc> </u2> <u3 id="3"> <name>Slow Regen</name> <desc>This is an upgrade Slow Regeneration</desc> </u3> <u4 id="4"> <name>Critical Chance</name> <desc>This is an upgrade Crit Chance</desc> </u4>

and after a lot of pain I am using this code to retrieve the information

         using (XmlReader xml = XmlReader.Create(Application.dataPath + Path.DirectorySeparatorChar + "Resources" + Path.DirectorySeparatorChar + "UpgradeDesc.xml")) {
             while (xml.Read()) {
                 if (xml.NodeType == XmlNodeType.Element) {
                     if (xml.GetAttribute("id") == upgrade.ToString()) {
                         using (XmlReader inner = xml.ReadSubtree()) {
 
                             bool foundDesc = false;
                             bool foundName = false;
                             string[] upgradeInfo = new string[3];
     
                             while (inner.Read()) {
                                 if (foundName) {
                                     upgradeInfo[0] = inner.Value;
                                 }
                                 if (foundDesc) {
                                     upgradeInfo[1] = inner.Value;
                                     upgradeInfo[2] = "info not from the XML here"; return upgradeInfo;
                                 }
                                 if (inner.LocalName == "name" && string.IsNullOrEmpty(upgradeInfo[0])) {
                                     foundName = true;
                                 }
                                 else {
                                     foundName = false;
                                 }
                                 if (inner.LocalName == "desc" && string.IsNullOrEmpty(upgradeInfo[1])) {
                                     foundDesc = true;
                                 }
                                 else {
                                     foundDesc = false;
                                 }
                             }
                         }
                     }
                 }
             }
             throw new System.Exception("No Upgrade of type " + (Upgrade.Upgrades)upgrade + " found!");
         }


While this works, I cant imagine this is the right way. How can I improve/optimize it ?

Thanks Majk-

Comment
Add comment · Show 1
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 Majk- · Aug 15, 2017 at 12:42 PM 0
Share

If you want to have a laugh/feel sad, look at the revisions for this topic...

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Majk- · Aug 18, 2017 at 02:31 PM

Ok, went for LINQ to XML

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

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

Related Questions

reference variables within my xml file. 0 Answers

A node in a childnode? 1 Answer

How to Stop Camera From Rotating when x,y,and z is equal to 90 degrees 1 Answer

How can I slow down a camera rotation and then stop the camera rotation when the camera is facing group of objects ? 0 Answers

[ARCore] How to hide Mesh Renderer of DetectedPlaneVisualizer script? 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