I am currently using XML files for character configurations and animation definitions, etc. Is there any way to include these files as a part of the .unity3d file so that our deployment is a single file?
Yup, you bet. Look at using a Resources folder.
I am working on this as well.
I am trying to get at the xml file so I can use XPATH on it.
I tried:
TextAsset questsFile = TextAsset)Resources.Load("XML/Quests", typeof(TextAsset));
string questText = questsFile.text;
System.IO.TextReader reader = new System.IO.StringReader(questText);
doc = new XPathDocument(reader);
nav = doc.CreateNavigator();
but no go… any ideas Higgy?
Ok this worked:
doc = new XPathDocument("Assets/Resources/XML/Quests.xml");
but I am betting (untested yet) that this will break when doing a build.
Ok thinking there is a bug in Mono (or its unsupported)
No matter how I try and read the xml (which is in a string)
Here is the exception:
XmlException: Text node cannot appear in this state. Line 1, position 1.
Player.Start () (at Assets\Standard Assets\Scripts\Player\Player.cs:30 at Mono.Xml2.XmlTextReader.ReadText (Boolean notWhitespace) [0x00000])
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)
System.Xml.XmlDocument.set_InnerXml (System.String value)
For example:
TextAsset questsFile = (TextAsset)Resources.Load("XML/Quests", typeof(TextAsset));
string questText = questsFile.text;
XmlDocument doc = new XmlDocument();
doc.InnerXml = questText;
XmlElement root = doc.DocumentElement;
tried other ways as well…
Can you post the XML file you are using?
Yeah it appears to be something with my xml. I will mess around with it and post if I still have issues. What’s odd is my xml app states its well formed.
Found the issue…if the xml was saved as UTF-8 it didn’t work, I saved it as ANSI and everything worked.
LOOOL !!!
I was just searching how to read local XML and found this topic, then tested it in my game and came to the same problem, then found the same solution and was going to post it when I saw you replied 30 seconds before ![]()
![]()
Nice to know I am 30 sec quicker than Batman - updating resume.
Sorry to reply to an old post but I have a problem: in mac I used an XML UTF-16 encoded and all worked fine but when I move my project on Windows I’ve got this error:
Text node cannot appear in this state. Line 1, position 1.
The whole exception is:
at Mono.Xml2.XmlTextReader.ReadText (Boolean notWhitespace) [0x00000]
at Mono.Xml2.XmlTextReader.ReadContent () [0x00000]
at Mono.Xml2.XmlTextReader.Read () [0x00000]
at System.Xml.XmlTextReader.Read () [0x00000]
at System.Xml.XmlDocument.ReadNodeCore (System.Xml.XmlReader reader) [0x00000]
at System.Xml.XmlDocument.ReadNode (System.Xml.XmlReader reader) [0x00000]
at System.Xml.XmlDocument.Load (System.Xml.XmlReader xmlReader) [0x00000]
I cannot save the XML file as ASCII because I need the UTF-16 Encoding.
How can I do?
Which function are you using to pull the XML text into the document?
SOLVED !!!
I used the XmlDocument.Load (XmlReader) function and it doesn’t work so I tried to use the XmlDocument.LoadXml (string) function and it works fine.
I haven’t understand if it is a Windows Mono bug.
I’m having the same problem. what can i do??
string fullPath = "http://localhost:49953/WebSite1/MallAssets/Data/1.xml";
WWW www = new WWW(fullPath);
yield return www;
XmlDocument doc = new XmlDocument();
doc.LoadXml([url]www.data[/url]);
string xmlString = doc.OuterXml.ToString();
// this printed the content correctly
Debug.Log("xmlString : " + xmlString);
// the above exception is raised from this line:
byte[] buffer = ASCIIEncoding.UTF8.GetBytes(xmlString);
.
.
.
this is the xml i’m reading:
<?xml version="1.0" encoding="utf-8"?>
<FloorInfo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Rooms>
<RoomInfo>
<RoomTag>001</RoomTag>
<Url>https://www.dunkindonuts.com/</Url>
</RoomInfo>
<RoomInfo>
<RoomTag>002</RoomTag>
<Url>http://www.nandos.co.uk/index.cfm</Url>
</RoomInfo>
<RoomInfo>
<RoomTag>003</RoomTag>
</RoomInfo>
</Rooms>
</FloorInfo>
thanx for any help.
firas,
Did you not read my post above about UTF-8 ?
thanx for all concern h2hjastermereel,
I just noticed the problem, utf8 actually worked…
but the compiler error was so vague!!
string fullPath = "http://localhost:49953/WebSite1/MallAssets/Data/1.xml";
WWW www = new WWW(fullPath);
yield return www;
XmlDocument doc = new XmlDocument();
doc.LoadXml([url]www.data[/url]);
string xmlString = doc.OuterXml.ToString();
byte[] buffer = ASCIIEncoding.UTF8.GetBytes(xmlString);
MemoryStream ms = new MemoryStream(buffer);
XmlReader reader = new XmlTextReader(ms);
XmlSerializer deserializer = new XmlSerializer(typeof(FloorInfo));
FloorInfo answer = (FloorInfo)deserializer.Deserialize(reader);
the problem was in the serialization which is all fixed above. my mistake was that i wrote:
(FloorInfo)deserializer.Deserialize(ms);
instead of:
(FloorInfo)deserializer.Deserialize(reader);
me dum dum!!
Hello everyone. I am having a similar problem and error. Ive tried encoding the xml file in utf-8, ANSI and unicode but i am still getting the:
XmlException: Text node cannot appear in this state. Line 1, position 1.
Mono.Xml2.XmlTextReader.ReadText (Boolean notWhitespace)
Im tryint to use c# and unity’s XML loader
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(filename); <-- string
heres what my XML looks like:
<?xml version="1.0" encoding="ANSI"?>
<Test type="MC" name="GCSE Test 1" numQuestions="2">
Note I changed the encoding on the XML file and saved it as the encoding type. is there anything else that can cause this error?
Edit: I am only trying to read the data by the way, not save it or do any complicated traversing and the files are held locally.
Thanks,
Johnty.
Well, if your XML is exactly the same as what you’ve posted, then the blank line at the start is a problem. The file has to begin with the <?xml… line without any whitespace ahead of it.
No the white space isnt there in the file. The xml file works loading it from flash and c++ using tinyXML so i know the file is valid.
If you are passing a filename in, you need to use Load, instead of LoadXml
string filename = "myxmlfile.xml";
XmlDocument.Load(filename)
If you are passing a string that is itself xml, then you use LoadXml
string xmlString = "<myxml>1</myxml>";
XmlDocument.LoadXml(xmlString)