x


Loading a large xml file (~200 multi-level nodes) into Unity

I'm trying to load a large xml file in for parsing from www.text, but the usual xml.LoadXml() does not work...

I've also tried:

        var stringReader:StringReader = new StringReader(w.text); 
        stringReader.Read(); // skip BOM
    	
	xml.LoadXml(stringReader.ReadToEnd());

I still get the following error:

XmlException: Text node cannot appear in this state.  Line 1, position 1.
Mono.Xml2.XmlTextReader.ReadText (Boolean notWhitespace)
Mono.Xml2.XmlTextReader.ReadContent ()
Mono.Xml2.XmlTextReader.Read ()
System.Xml.XmlTextReader.Read ()
System.Xml.XmlDocument.ReadNodeCore (System.Xml.XmlReader reader)
System.Xml.XmlDocument.ReadNode (System.Xml.XmlReader reader)
System.Xml.XmlDocument.Load (System.Xml.XmlReader xmlReader)
System.Xml.XmlDocument.LoadXml (System.String xml)
gniptwitter+$fetchTweets$42+$.MoveNext () (at /gniptwitter.js:35)

The data file is from an api so I can't change its format. However, it is valid XML. It might be UTF8 or some odd encoding that's causing Unity to balk at XML-parsing it...

more ▼

asked Aug 30 '11 at 08:51 AM

ina gravatar image

ina
3.3k 492 551 602

Does it actual have a Byte Order Marker? I'd start by not doing that extra read.

Aug 30 '11 at 10:30 AM Waz

i actually tried it straight on with loadXML without that .read ... but errors galore. google'd around, found that the .read worked for some, but apparently not here :-\

Aug 31 '11 at 07:25 AM ina
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

This does happen due to encoding issues; if you use a standard textreader and read one character at a time and then print the characters, you'll notice that the stream is littered with spaces and possibly questionmarks if the StringReader fails to decode it. If your xml is in a file on the harddrive, the usual way to load it is to call XmlDocument.Load. Not XmlDocument.LoadXml, mind you. XmlDocument.Load takes care of parsing the XML for you, while XmlDocument.LoadXml expects a string that contains raw XML to make a tree out of.

In your case, where a file is very large, calling XmlDocument.Load from the mainthread will cause the thread to block until loading completes, and you'll experience a holdup in Unity's rendering. To get around that, you can have a separate thread load the xml for you and signal the mainthread when it's complete, or you can streamread the XML (using an XmlReader), and only call its Read()-method a few times every frame.

more ▼

answered Aug 30 '11 at 09:51 AM

CHPedersen gravatar image

CHPedersen
6k 13 22 61

200 nodes is tiny, so I doubt a thread is needed.

Aug 30 '11 at 10:29 AM Waz

He says "200 multi-level nodes". I assume this means each one has multiple children and grandchildren of its own.

Aug 30 '11 at 10:59 AM CHPedersen

That wouldn't be valid XML then, since there can be only one root node. (rimshot!)

Aug 30 '11 at 11:04 AM Waz

Oh, you're such a nitpicker, Warwick. :)

Aug 30 '11 at 11:13 AM CHPedersen

1) i am not sure if it is the loading being blocked, as would loading being blocked result in a red-marked error in the console, as pasted above? 2) and, i'm not sure how to load the separate threads - would calling yield on the loadXml work? (@warwick and apologies, i often use words loosely, but the meaning is there! ;) )

Aug 31 '11 at 07:31 AM ina
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x527
x256
x150
x97
x1

asked: Aug 30 '11 at 08:51 AM

Seen: 1957 times

Last Updated: Aug 31 '11 at 11:07 AM