Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Include Files in build

Discussion in 'Editor & General Support' started by abarrett86, Sep 30, 2009.

  1. abarrett86

    abarrett86

    Joined:
    Sep 9, 2009
    Posts:
    6
    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?
     
  2. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    Yup, you bet. Look at using a Resources folder.
     
  3. Mixality_KrankyBoy

    Mixality_KrankyBoy

    Joined:
    Mar 27, 2009
    Posts:
    737
    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:

    Code (csharp):
    1.  
    2. TextAsset questsFile = TextAsset)Resources.Load("XML/Quests", typeof(TextAsset));
    3. string questText = questsFile.text;
    4. System.IO.TextReader reader = new System.IO.StringReader(questText);
    5. doc = new XPathDocument(reader);
    6. nav = doc.CreateNavigator();
    7.  
    but no go... any ideas Higgy?
     
  4. Mixality_KrankyBoy

    Mixality_KrankyBoy

    Joined:
    Mar 27, 2009
    Posts:
    737
    Ok this worked:

    Code (csharp):
    1.  
    2. doc = new XPathDocument("Assets/Resources/XML/Quests.xml");
    3.  
    but I am betting (untested yet) that this will break when doing a build.
     
  5. Mixality_KrankyBoy

    Mixality_KrankyBoy

    Joined:
    Mar 27, 2009
    Posts:
    737
    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:

    Code (csharp):
    1.  
    2. TextAsset questsFile = (TextAsset)Resources.Load("XML/Quests", typeof(TextAsset));
    3. string questText = questsFile.text;
    4. XmlDocument doc = new XmlDocument();
    5. doc.InnerXml = questText;
    6. XmlElement root = doc.DocumentElement;
    7.  
    tried other ways as well...
     
  6. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    Can you post the XML file you are using?
     
  7. Mixality_KrankyBoy

    Mixality_KrankyBoy

    Joined:
    Mar 27, 2009
    Posts:
    737
    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.
     
  8. Mixality_KrankyBoy

    Mixality_KrankyBoy

    Joined:
    Mar 27, 2009
    Posts:
    737
    Found the issue..if the xml was saved as UTF-8 it didn't work, I saved it as ANSI and everything worked.
     
  9. p-lux

    p-lux

    Joined:
    Oct 21, 2008
    Posts:
    59
    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 :)

    :) :) :)
     
  10. Mixality_KrankyBoy

    Mixality_KrankyBoy

    Joined:
    Mar 27, 2009
    Posts:
    737
    Nice to know I am 30 sec quicker than Batman - updating resume.
     
  11. Apache

    Apache

    Joined:
    Sep 26, 2008
    Posts:
    134
    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:
    Code (csharp):
    1. Text node cannot appear in this state. Line 1, position 1.
    The whole exception is:
    Code (csharp):
    1. at Mono.Xml2.XmlTextReader.ReadText (Boolean notWhitespace) [0x00000]
    2.   at Mono.Xml2.XmlTextReader.ReadContent () [0x00000]
    3.   at Mono.Xml2.XmlTextReader.Read () [0x00000]
    4.   at System.Xml.XmlTextReader.Read () [0x00000]
    5.   at System.Xml.XmlDocument.ReadNodeCore (System.Xml.XmlReader reader) [0x00000]
    6.   at System.Xml.XmlDocument.ReadNode (System.Xml.XmlReader reader) [0x00000]
    7.   at System.Xml.XmlDocument.Load (System.Xml.XmlReader xmlReader) [0x00000]
    8.  
    I cannot save the XML file as ASCII because I need the UTF-16 Encoding.

    How can I do?
     
  12. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    Which function are you using to pull the XML text into the document?
     
  13. Apache

    Apache

    Joined:
    Sep 26, 2008
    Posts:
    134
    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.
     
  14. firas darwiche

    firas darwiche

    Joined:
    Oct 4, 2006
    Posts:
    130
    I'm having the same problem. what can i do??

    Code (csharp):
    1.  
    2.  
    3. string fullPath = "http://localhost:49953/WebSite1/MallAssets/Data/1.xml";
    4.  
    5. WWW www = new WWW(fullPath);
    6. yield return www;
    7. XmlDocument doc = new XmlDocument();
    8. doc.LoadXml([url]www.data[/url]);
    9.  
    10. string xmlString = doc.OuterXml.ToString();
    11.  
    12. // this printed the content correctly
    13. Debug.Log("xmlString : " + xmlString);
    14.  
    15. // the above exception is raised from this line:
    16. byte[] buffer = ASCIIEncoding.UTF8.GetBytes(xmlString);
    17. .
    18. .
    19. .
    20.  
    this is the xml i'm reading:

    Code (csharp):
    1.  
    2. <?xml version="1.0" encoding="utf-8"?>
    3. <FloorInfo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    4.   <Rooms>
    5.     <RoomInfo>
    6.       <RoomTag>001</RoomTag>
    7.       <Url>https://www.dunkindonuts.com/</Url>
    8.     </RoomInfo>
    9.     <RoomInfo>
    10.       <RoomTag>002</RoomTag>
    11.       <Url>http://www.nandos.co.uk/index.cfm</Url>
    12.     </RoomInfo>
    13.     <RoomInfo>
    14.       <RoomTag>003</RoomTag>
    15.     </RoomInfo>
    16.   </Rooms>
    17. </FloorInfo>
    18.  
    thanx for any help.
     
  15. Mixality_KrankyBoy

    Mixality_KrankyBoy

    Joined:
    Mar 27, 2009
    Posts:
    737
    firas,

    Did you not read my post above about UTF-8 ?
     
  16. firas darwiche

    firas darwiche

    Joined:
    Oct 4, 2006
    Posts:
    130
    thanx for all concern h2hjastermereel,

    I just noticed the problem, utf8 actually worked...
    but the compiler error was so vague!!

    Code (csharp):
    1.  
    2. string fullPath = "http://localhost:49953/WebSite1/MallAssets/Data/1.xml";
    3.  
    4. WWW www = new WWW(fullPath);
    5. yield return www;
    6. XmlDocument doc = new XmlDocument();
    7. doc.LoadXml([url]www.data[/url]);
    8.  
    9. string xmlString = doc.OuterXml.ToString();
    10. byte[] buffer = ASCIIEncoding.UTF8.GetBytes(xmlString);
    11. MemoryStream ms = new MemoryStream(buffer);
    12. XmlReader reader = new XmlTextReader(ms);
    13.  
    14. XmlSerializer deserializer = new XmlSerializer(typeof(FloorInfo));
    15. FloorInfo answer = (FloorInfo)deserializer.Deserialize(reader);
    16.  
    the problem was in the serialization which is all fixed above. my mistake was that i wrote:

    Code (csharp):
    1. (FloorInfo)deserializer.Deserialize(ms);
    instead of:

    Code (csharp):
    1. (FloorInfo)deserializer.Deserialize(reader);
    :oops: me dum dum!!
     
  17. johnty

    johnty

    Joined:
    Mar 18, 2010
    Posts:
    3
    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

    Code (csharp):
    1.  
    2. XmlDocument xmlDoc = new XmlDocument();
    3. xmlDoc.LoadXml(filename); <-- string
    4.  
    heres what my XML looks like:

    Code (csharp):
    1.  
    2. <?xml version="1.0" encoding="ANSI"?>
    3. <Test type="MC" name="GCSE Test 1" numQuestions="2">
    4.  
    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.
     
  18. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    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.
     
  19. johnty

    johnty

    Joined:
    Mar 18, 2010
    Posts:
    3
    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.
     
  20. samirez

    samirez

    Joined:
    Feb 21, 2010
    Posts:
    3
    If you are passing a filename in, you need to use Load, instead of LoadXml

    Code (csharp):
    1. string filename = "myxmlfile.xml";
    2. XmlDocument.Load(filename)
    If you are passing a string that is itself xml, then you use LoadXml
    Code (csharp):
    1. string xmlString = "<myxml>1</myxml>";
    2. XmlDocument.LoadXml(xmlString)
    3.  
     
  21. johnty

    johnty

    Joined:
    Mar 18, 2010
    Posts:
    3
    I got them mixed up, thought it was the other way around :) ...... only one thing for this /facepalm

    edit: ok so it is working now. This loads xml files from the root project folder and is working with utf-8 encoding
     
  22. Jehu

    Jehu

    Joined:
    Aug 19, 2010
    Posts:
    16
    Your XML is not well formed.
    You opened the root element <Test ... >
    But you didnt close it with

    </Test>
     
  23. macy

    macy

    Joined:
    Aug 25, 2011
    Posts:
    3
    I had the same error.
    My problem was the xml file I created in TextEdit in MAC. Somehow this text editor added so many texts into my xml, made it unreadable.

    The Solution was, open the xml in an pure code editor, clean out all the hidden text added by TextEdit.