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
1
Question by MichaelTaylor3d · Aug 29, 2012 at 12:14 AM · wwwxml

Loading xml data from www.text

I can load a comma deliminated version of my xml data into my class fine, but in order to make it cleaner I wanted to output the data from my server in xml format and then load it in unity.

In none xml format, I use WWW.text and then parse it. so Im assuming that to get xml from the web I would use the same thing.

I can also load an xml file fine using the following code:

     public static object Load(Type type, string path)
     {
         object data;
         try
         {
             StreamReader reader = new StreamReader(path);
             XmlSerializer xml = new XmlSerializer(type);
             data = xml.Deserialize(reader);
             reader.Close ();
         }
         catch
         {
             data = null;
         }
         
         return data;
     }

However getting an xml schema from www.text and then deserializing it has not worked for me. Ive tried multiple derivative of the above code. Could some one aid me in collecting xml from www.text?

the output from my server is standard xml format.

EDIT:

Here is a slightly modified version that has given me the most luck. It seems it is loading the xml file because an exception isnt thrown in my catch block, although the class values its supposed to populate are still showing up null. Again, when i use the version that loads from files it works perfectly.

 public static object LoadXML(Type type, string url)
     {
         object data;
 
         try
         {
             using (StringReader reader = new StringReader(url))
             {
                 XmlSerializer xml = new XmlSerializer(type);
                 data = xml.Deserialize(reader);
                 reader.Close();
             }
         }
         catch(Exception exception)
         {
             Debug.LogError (exception);
             data = type.GetConstructor(Type.EmptyTypes).Invoke(null);
         }
         
         Debug.Log (data.ToString());
         return data;
     }
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
Best Answer

Answer by MichaelTaylor3d · Aug 30, 2012 at 05:13 PM

The issue ended up being a problem with uppercase and lower case. But this peice of code is what exposed it almost immediately.

Using xmlserializers event handlers, I was able to nail down the problem:

     private void Serializer_UnknownElement(object sender, XmlElementEventArgs e)
     {
       Debug.Log("Unknown Element");
       Debug.Log(e.Element.Name + " " + e.Element.InnerXml);
       Debug.Log("LineNumber: " + e.LineNumber);
       Debug.Log("LinePosition: " + e.LinePosition);
 
       gtMember x  = (gtMember) e.ObjectBeingDeserialized;
       Debug.Log(x.AccountName_a);
       Debug.Log(sender.ToString());
    }

And then insert this into the xml string serializer from above: serializer.UnknownElement+=new XmlElementEventHandler(Serializer_UnknownElement);

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

Answer by Pandiux · Aug 29, 2012 at 05:13 AM

Its possible www.text sometimes comes with some unwanted characters. Try using replace in the www.text, one that has always got me is the indent character, try:

string cleanedResponse = www.text.Replace("\\t", "");

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 MichaelTaylor3d · Aug 30, 2012 at 01:49 AM 0
Share

I incorporated that into the code, still doesnt seem to be working, although I like having it in there as a way to protect it from those errors in the future. I really think that Its that i dont have the xml deserializer coded properly to handle a string. The version I have working handles files fine though.

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to read xml file on Android using www and StreamingAssets? 2 Answers

ArgumentException: Path is empty while saving data in an XML 0 Answers

WWW is not ready downloading yet? 1 Answer

WWW requests are exponentially slow on large web pages (xml) 1 Answer

Parsing large XML files on-the-fly, best practice? 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