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 Kirki_333 · Apr 20, 2020 at 02:49 PM · xmlexception

XmlException: Root element is missing when deserializing

I am trying to deserialize an xml file. My code is based on answers I've read, I googled for a solution end everyone keeps stating to check the xml to see if it has a root but I have no problem with this. This is my xml:

 <?xml version="1.0" encoding="utf-8"?>
 <tips>
   <tip>
 All nutrition facts labels have the same format, with serving size and calories listed on top. Below this:
 ●    List of essential nutrients including: total fat, cholesterol, sodium, potassium, total carbohydrates and finally protein.
 ●    Below that: the essential nutrients, some vitamins and minerals.
 ●    Finally, the ingredient list.
 The serving size for the product helps you determine if you actually consume the amount listed as the serving size. For example, the serving size for ready-to-eat cereal is one cup. If you eat two cups of cereal, you would need to multiply the numbers on the nutrition facts label by two.
 </tip>
   
   <tip>
 The total fat is important for determining what types of fat the product contains. Fats such as mono and polyunsaturated fats are more heart healthy fats while saturated and trans-fats are detrimental to overall health. The American Heart Association (AHA) recommends avoiding products that contain
 any industrially manufactured trans-fat, meaning “partially hydrogenated oils”.
   </tip>
 </tips>

And here is my code to find and deserialize the file:

  string absolute_path = Application.streamingAssetsPath + file_path + ".xml";
         Debug.Log("This is the file path" + absolute_path);
 
         WWW www = new WWW(absolute_path);
 
         while (!www.isDone)
         {
 
         }
 
         XmlSerializer serializer = new XmlSerializer(typeof(Tips));
         MemoryStream stream = new MemoryStream(www.bytes);
 
         if(stream != null)
         {
             Debug.Log("Stream is not null");
             tips = serializer.Deserialize(stream) as Tips;
 
         }
 
         stream.Close();

This is the Tips class:

 [XmlRoot("tips")]
 public class Tips
 {
     [XmlElement("tip")]
     public List<string> tip { get; set; }
 }

Any help will be greatly appreciated :)

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 Kirki_333 · Apr 20, 2020 at 06:56 PM 0
Share

I forgot to add that I am running this code on my android device after installing the apk. The code run on my pc successfully but if I install the apk, the error I stated comes up.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Priyanka-Rajwanshi · Apr 20, 2020 at 06:36 PM

@Kirki_333 Try using StringReader.

     filePath = Path.Combine(Application.persistentDataPath, "filename.xml");
     if (File.Exists(loadedFile))
     {
         string text = File.ReadAllText(loadedFile);
         XmlSerializer serializer = new XmlSerializer(typeof(Tips));
         using (StringReader reader = new StringReader(text))
         {
             Tips tips = serializer.Deserialize(reader) as Tips;
         }
     }

For complete documentation of how to parse Xml, check this link.

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 Kirki_333 · Apr 20, 2020 at 07:30 PM 0
Share

I tried your code, now it gives me a null reference because it can't find the file. The variable loadedFile has this value jar:file:///data/app/com.$$anonymous$$tsa.Healthy_$$anonymous$$ev6-2/base.apk!/assets/understanding_nutrition_tips.xml

Should the path have that format?

avatar image Priyanka-Rajwanshi Kirki_333 · Apr 21, 2020 at 07:00 AM 0
Share

use filePath = Path.Combine(Application.persistentDataPath, "filename.xml"); I have checked it in Android Phone and it works fine. I have updated original answer accordingly.

avatar image
0

Answer by Bunny83 · Apr 20, 2020 at 03:05 PM

Well such issues are usually related to the actual data you're putting through the XmlSerializer. Note that the XmlSerializer does not really work well with BOMs at the front of your file. While most applications just ignore it or use it to interpret the byte order of the file, the XmlSerializer will choke on it. So when editing your xml file, make sure you save it without a byte order mark. Even it should be obvious it's generally recommended to save your file with utf8 since it is the text encoding that works best on all platforms.


If you're on windows and even using notepad to edit your xml file, just resave it and select UTF8 (without BOM) as the encoding and you shouldn't have any issues. Any notable text editor will allow you to omit a BOM.


If you get that file from somewhere else you may want to analyze the start of the byte stream manually and remove the BOM before feeding it to the serializer.

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 Kirki_333 · Apr 20, 2020 at 06:54 PM 0
Share

After reading your answer, I opened my xml file with Notepad++ and checked at the bottom right corner if it says UTF-8-BO$$anonymous$$ but it seems it is alright. It says it's UTF-8. Is there a possibility the BO$$anonymous$$ exists and is unrecognisable by a text editor? Also, I didn't get the file from somewhere, I created it myself.

avatar image Bunny83 Kirki_333 · Apr 20, 2020 at 08:24 PM 0
Share

The byte order mark is not a printable character. It's a control character that is used by text parsers to know the byte order used to encode the following text. For UTF8 this is completely useless since UTF8 is immune to different byte orders. However from the unicode point of view it's not explicitly forbidden to include a byte order mark, they actuall recommend to not including it as you can read here.


When you use Notepad++ you can change your encoding settings in the preferences. I quickly switched the language from german to english and made a screenshot:

Note that changing the preferences will not affect any file that already has a byte order mark. To remove it in Notepad++ you should use the format / encoding menu at the top. Note that you have to resave the file when you changed the format / encoding

This is the difference when I view my test file with a hex editor

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

129 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 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

XML Load unauthorized access 2 Answers

A node in a childnode? 1 Answer

Unity Crash - Access Violation (0xc0000005) error 1 Answer

XmlWriter.Create exception, "Could not find the file..." on Android 0 Answers

Can not reference System.Xml.Linq 3 Answers


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