Two ways to read XML

Source: Internet
Author: User

The contents of the 1.xml file are as follows:

<?xml version= "1.0" encoding= "Utf-8"?>
<info>
<book id = "B1" lang = "en" >
<name>c++</name>
<price>555</price>
</book>
<book id = "B2" lang = "en" >
<name>c#</name>
<price>666</price>
</book>
</info>

This file can be placed under the resources or streamingassets, so there are two ways to read the path, read as follows

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

public class Readxml:monobehaviour {


<summary>
Resources to read XML under the method
</summary>
void Start ()
//{

The reading method under the resources
Textasset Textasset = resources.load ("XMLFile1", typeof (Textasset)) as Textasset;
ReadXML (new MemoryStream (textasset.bytes));
//}

<summary>
Streamingassets The following method of reading XML
</summary>
<returns></returns>
IEnumerator Start ()
{
www www = new www ("file://" + Application.streamingassetspath + "/xmlfile1.xml");
yield return www;

ReadXML (new MemoryStream (www.bytes));
Www.Dispose ();
}

Update is called once per frame
void Update ()
{

}

void ReadXML (Stream stream) {
XmlDocument xmldoc = new XmlDocument ();
Xmldoc.load (stream);

XmlNode info = xmldoc.selectsinglenode ("info");
foreach (XmlNode node in info. ChildNodes)
{
String id = node. attributes["id"]. InnerText;
String lang = node. attributes["Lang"]. InnerText;

String name = node. selectSingleNode ("name"). InnerText;

Debug.Log (node. Name + "/" + "id =" + id + "lang =" + lang + "name =" + name);
}
}
}

Two ways to read XML

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.