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 Ratboy601 · Jan 03, 2016 at 08:59 AM · xmlruntime-error

Reading Xml files, InvalidCastException

Hi, I know there's lots of questions out there about invalid cast exceptions, but I couldn't find any that are cluing me in enough on my problem. I got this script from the forums for adding multiple language support, and its a bit more advanced than my current skill level. The line from SetLanguage()

  var xmlItem = (XmlElement)elemEnum.Current;

is giving me the InvalidCastException runtime error when I call the function from another script like this:

 langClass.setLanguage (Path.Combine (Application.dataPath, currentLevel + currentLanguage + ".xml"), currentLanguage);

Here's the whole script for your reference. If I can post anything else to help, please let me know.

  using System;
     using System.Collections;
     using System.IO;
     using System.Xml;
      
     using UnityEngine;
      
     public class Lang
     {
         private Hashtable Strings;
      
         public Lang ( string path, string language, bool web) {
             if (!web) {
                 setLanguage(path, language);
             } else {
                 setLanguageWeb(path, language);
             }
         }
      
         public void setLanguage ( string path, string language) {
             var xml = new XmlDocument();
             xml.Load(path);
          
             Strings = new Hashtable();
             var element = xml.DocumentElement[language];
             if (element != null) {
                 var elemEnum = element.GetEnumerator();
                 while (elemEnum.MoveNext()) {
                     var xmlItem = (XmlElement)elemEnum.Current;
                     Strings.Add(xmlItem.GetAttribute("name"), xmlItem.InnerText);
                 }
             } else {
                 Debug.LogError("The specified language does not exist: " + language);
             }
         }
      
         public void setLanguageWeb ( string xmlText, string language) {
             var xml = new XmlDocument();
             xml.Load(new StringReader(xmlText));
          
             Strings = new Hashtable();
             var element = xml.DocumentElement[language];
             if (element != null) {
                 var elemEnum = element.GetEnumerator();
                 while (elemEnum.MoveNext()) {
                     var xmlItem = (XmlElement)elemEnum.Current;
                     Strings.Add(xmlItem.GetAttribute("name"), xmlItem.InnerText);
                 }
             } else {
                 Debug.LogError("The specified language does not exist: " + language);
             }
         }
      
         /*
         Access strings in the currently selected language by supplying this getString function with
         the name identifier for the string used in the XML resource.
      
         Example:
         XML file:
         <languages>
             <English>
                 <string name="app_name">Unity Multiple Language Support</string>
                 <string name="description">This script provides convenient multiple language support.</string>
             </English>
             <French>
                 <string name="app_name">Unité Langue Soutien Multiple</string>
                 <string name="description">Ce script fournit un soutien multilingue pratique.</string>
             </French>
         </languages>
      
         JavaScript:
         var appName : String = langClass.getString("app_name");
         */
         public string getString (string name) {
             if (!Strings.ContainsKey(name)) {
                 Debug.LogError("The specified string does not exist: " + name);
              
                 return "";
             }
      
             return (string)Strings[name];
         }
      
     }
 
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

0 Replies

· Add your reply
  • Sort: 

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

xml loading in unity andriod 1 Answer

Why am I geting null reference exception while reading xml file? 0 Answers

Inserting specific words into descriptions stored in XML 0 Answers

after i imported admob to my android unity project and edited the code, error occured,after i imported admob to my android unity project, error occured 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