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 crazyKnight · May 22, 2012 at 05:03 AM · xmlparsing

Retrive value from xml

Hi all,

i want to retrive some values from a XML,here is the XML

 <?xml version="1.0"?>
 <transforms>
   <Happy>
     <FacePosition>
       <x>7</x>
       <y>8</y>
       <z>9</z>
     </FacePosition>
     <FaceRotation>
       <x>1</x>
       <y>2</y>
       <z>3</z>
     </FaceRotation>
     <EyeRotation>
       <x>4</x>
       <y>5</y>
       <z>6</z>
     </EyeRotation>
   </Happy>
   <nervous>
     <FacePosition>
       <x>1</x>
       <y>2</y>
       <z>3</z>
     </FacePosition>
     <FaceRotation>
       <x>4</x>
       <y>5</y>
       <z>6</z>
     </FaceRotation>
     <EyeRotation>
       <x>7</x>
       <y>8</y>
       <z>9</z>
     </EyeRotation>
   </nervous>
 </transforms>


Here's the method to retrive the values

 public static Vector3 LoadFromXml(string elementName,string typename)
     {
         float X = 0;
         float Y = 0;
         float Z = 0;
         
         string filepath = Application.dataPath + @"/Resources/Data.xml";
         //string filepath = Resources.Load("Data");
         
         XmlDocument xmlDoc = new XmlDocument(); 
         
         if(File.Exists (filepath))
         {
             xmlDoc.Load(filepath); 
                 
             XmlNodeList transformList = xmlDoc.GetElementsByTagName(elementName);
         
             foreach (XmlNode transformInfo in transformList)
             {
                 XmlNodeList transformcontent = transformInfo.ChildNodes;
                 
                 foreach (XmlNode transformItens in transformcontent) 
                 {
                     if(transformItens.Name == "x")
                     {
                             X = float.Parse(transformItens.InnerText); // convert the strings to float and apply to the Y variable.
                     }
                     if(transformItens.Name == "y")
                     {
                              Y = float.Parse(transformItens.InnerText); // convert the strings to float and apply to the Y variable.
                     }
                     if(transformItens.Name == "z")
                     {
                             Z = float.Parse(transformItens.InnerText); // convert the strings to float and apply to the Y variable.
                     }
                 }
             }
         
         }
         return new Vector3(X,Y,Z);;
     }


here element name is the name of the state which could be happy or nervous and type name is the name of the sub element facerotation or faceposition or eye rotation.i want to retrive coordinates for a specific state and its subelement.

please help me out i am completely new to XML

Comment
Add comment · Show 3
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 crazyKnight · May 22, 2012 at 05:14 AM 0
Share

guys please help ...

avatar image flamy · May 22, 2012 at 05:51 AM 0
Share

wat is the problem error? null ref. or something else?!?! you just posted the code and didn't say way the problem is!

avatar image crazyKnight · May 22, 2012 at 06:16 AM 0
Share

@flamy the problem was i was not able to retrive the specific vallues as i am completely new to xml,i have solved the problem and will post the solution.

1 Reply

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

Answer by crazyKnight · May 22, 2012 at 06:17 AM

solved myself

heres the solution

 public static Vector3 LoadFromXml(string elementName,string typename,int stage)
 {
     float X = 0;
     float Y = 0;
     float Z = 0;
     string p1 = "x";
     string p2 = "y";
     string p3 = "z";

     string filepath = Application.dataPath + @"/Resources/Data.xml";
     //string filepath = Resources.Load("Data");
     
     XmlDocument xmlDoc = new XmlDocument(); 
     
     if(File.Exists (filepath))
     {
         xmlDoc.Load(filepath); 
             
         XmlNodeList transformList = xmlDoc.GetElementsByTagName(elementName);
     
         foreach (XmlNode transformInfo in transformList)
         {
             XmlNodeList transformcontent = transformInfo.ChildNodes;
             
             foreach (XmlNode transformItens in transformcontent) 
             {
                 if(transformItens.Name == typename)
                 {
                     XmlNodeList newtransformList = transformItens.ChildNodes;
                         
                         foreach (XmlNode newtransformItens in newtransformList)
                         {
                                 if(newtransformItens.Name == "x")
                                 {
                                         X = float.Parse(newtransformItens.InnerText); // convert the strings to float and apply to the Y variable.
                                 }
                                 if(newtransformItens.Name == "y")
                                 {
                                          Y = float.Parse(newtransformItens.InnerText); // convert the strings to float and apply to the Y variable.
                                 }
                                 if(newtransformItens.Name == "z")
                                 {
                                         Z = float.Parse(newtransformItens.InnerText); // convert the strings to float and apply to the Y variable.
                                 }
                         } 
                 }
             }
         }
     
     }
     return new Vector3(X,Y,Z);;
 }
Comment
Add comment · Show 1 · 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 jacobtw · May 27, 2013 at 02:01 PM 0
Share

This is exactly what I'm looking for I can't get it to work though:(

Is this a working example or did you leave something out?

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

XML parsing c# in unity 1 Answer

Saving serializable class list at runtime. 1 Answer

Easy XML Parsing 4 Answers

Parsing and displaying KML 0 Answers

Problem with XML parse 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