Unity3d Read and write XML files

Source: Internet
Author: User

It is a good idea to use an XML file to store data when a program sometimes needs to read data from text or save data to a file. Here's how to use C # to read and write XML files in Unity3d.

1, need to introduce the package

Using Unityengine;
Using System.Collections;
Using System.Xml;

2. Edit your XML file

<?xml version= "1.0" encoding= "Utf-8"?>
<RoleRoot>
  
  <role faction= "0" >
    < Baseattribute   grade= "1" force= "2" spirit= "1" agility= "1" endurance= "1" wisdom= "1"/>
    <fightattribute  hp= "1" mp= "1" generalhurt= "1" generaldefense= "1" skillhurt= "1"
             skilldefense= "1" generalhurtfactor= "1" Generaldefensefactor= "1"
                     skillhurtfactor= "1" skilldefensefactor= "1"/>
    <rateattribute   Violencerate= "0" killrate= "0" breakdefenserate= "0" duckrate= "0"
                     withstandrate= "0" hitrate= "0"/>
    < Valueattribute  attackorder= "0" money= "0" goldbullions= "0" bags= "0" achievement= "0"
                     experience= "0" vitality= "0"/>
  </Role>
</RoleRoot>
3. Read XML data

XmlDocument xmldoc = new XmlDocument ();      
        Textasset Textasset = (textasset) resources.load ("Createrole");
        Xmldoc.loadxml (Textasset.text);
        Xmldoc.loadxml (Application.datapath + @ "\createrole.xml");
        XmlNodeList nodelist = xmldoc.selectnodes ("Roleroot/role");
        XmlNode node; Switch (faction) {case faction.
                Monk:node = nodelist[0];
            Break Case faction.
                Taoist:node = nodelist[1];
            Break Case faction.
                Flower:node = nodelist[2];
            Break
                Default:node = nodelist[3];
        Break }//xmlnode Baseattribute=node.
        SelectNodes ("Baseattribute"); XmlNode baseattribute = node.
        Childnodes[0]; Grade = Xmlconvert.toint32 (baseattribute.attributes["grade").
        Value); force = Xmlconvert.toint32 (baseattribute.attributes["Force").
  Value);      Spirit = Xmlconvert.toint32 (baseattribute.attributes["spirit").
        Value); Agility = Xmlconvert.toint32 (baseattribute.attributes["Agility").
        Value); Endurance = Xmlconvert.toint32 (baseattribute.attributes["endurance").
        Value); Wisdom = Xmlconvert.toint32 (baseattribute.attributes["wisdom"). Value);

This error may occur when loading an XML file Xmldoc.loadxml ()

Xmlexception:text node cannot appear in the. Line 1, Position 1.

Mono.Xml2.XmlTextReader.ReadText (Boolean notwhitespace)

Mono.Xml2.XmlTextReader.ReadContent () Mono.Xml2.XmlTextReader.Read ()
This error occurs because when Unity3d loads an XML file, the XML file must be saved in a UTF-8 encoded format, and the first two bytes (BOM) must be removed to identify the UTF-8. You can then select some editing tools to save as UTF-8 (some tools will default to add a BOM identifier for the UTF-8 code), such as you can export XML using Eclipse, so that you don't have a BOM ID. There are other solutions: Give a link: http://answers.unity3d.com/questions/10904/xmlexception-text-node-canot-appear-in-this-state.html



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.