求助: 如何用XmlTextReader类读取外部载入的Xml文件? 急

5 replies [最后一篇]
cartoonboy
User offline. Last seen 51 周 3 天 ago. Offline
版主
Joined: 05/10/2010
Points: 316

我用外部载入的方式载入了xml文件.如下:

var url = "file://the1.xml";

var www : WWW = new WWW(url);

现在要用var reader : XmlTextReader = new XmlTextReader()的形式读取xml文件的内容.请高手指点!

另:网上有从Resources载入的例子,但是从内部载入xml获取数据几乎没有现实意义.

rmpci
User offline. Last seen 34 周 4 天 ago. Offline
注册用户
Joined: 03/28/2010
Points: 20

如果要用:XmlTextReader的话,请先导入:system.xml    和   system.io再写下面的....

var xmlR : XmlTextReader = new XmlTextReader(" http://127.0.0.1/the1.xml");

rmpci
User offline. Last seen 34 周 4 天 ago. Offline
注册用户
Joined: 03/28/2010
Points: 20

xml文件
<?xml version="1.0" encoding="utf-8" ?>
<TaskList>
  <item>
    <title>Learn English</title>
    <description>Memory 30 words every day.
                Read some English Newspaper, and so on.
        </description>
  </item>
  <item>
          <title>Study Unity 3D</title>
        <description>The Unity 3D is so hard to grasp!</description>
  </item>
  <item>
          <title>Study 3Dmax</title>
        <description>To learn Unity 3D ,don't understand 3Dmax is not work
        </description>
        </item>
        <item>
                <title>Asp.Net</title>
                <description>Asp.Net 2.0 is easy to exploit Web Form</description>
        </item>
        <item>
                <title>Overview: Common Operations</title>
                <description>Next: TimeMost game object manipulations are done either through the game object's Transform and/or Rigidbody. These are accessible inside behaviour scripts through the member variables transform and rigidbody respectively. So if you wanted to rotate an object 5 degrees around its Y axis every frame you could write the following:
                </description>
        </item>
        <item>
                <title>中文</title>
                <description>显示中文,中华人民共和国中华人民共和国中华人民共和国。</description>
        </item>
</TaskList>

Unity JAVASCRIP 脚本

import System.Xml;
import System.Data;
import System.IO;

//var table :DataTable;
private var _xmlDoc : XmlDocument;
private var _xnNode : XmlNode;

var _options : GUILayoutOption[];

var _fScroll1 : float;
var _fScroll2 : float;
var _sDocPath : String;
var _guiSkin : GUISkin;

var _windowRect : Rect;
var _sDescription : String ;

var _List : Array;

function Start()
{
        _fScroll1 = 0;
        _fScroll2 = 0;
        _windowRect = new Rect(10,10 ,300,200);
        _List = new Array();
        _sDocPath = "D:\\My Documents\\Try\\Assets\\Data\\TaskList.xml";
        _xmlDoc = new XmlDocument();
        _xmlDoc.Load(_sDocPath);
       
        _xnNode = _xmlDoc.SelectSingleNode("TaskList");
        var i : int ;
        var item : String[];
        for( i = 0 ; i< _xnNode.ChildNodes.Count ; i++)
        {
                item = new String[2];
                item[0] = _xnNode.ChildNodes.ChildNodes[0].InnerText ;
                item[1] = _xnNode.ChildNodes.ChildNodes[1].InnerText ;
                _List.Add(item);
        }
}

function ReturnList(){
        return _List;
}

function OnGUI()
{
        GUI.skin = _guiSkin;
        _windowRect = GUI.Window( 0 ,_windowRect, CreatListWindow,"TaskList");
}

function CreatListWindow( windowId : int)
{
        var i:int;
        var readitem : String[] ;
        for( i = 0 ; i < _List.length ; i++)
        {
                readitem = _List;
                if(GUI.Button(Rect(10 , 20 * i+20, 100 ,18), readitem[0]))
                {
                        _sDescription = readitem[1];
                }
        }
        _fScroll1 = GUI.VerticalScrollbar(Rect(120,20,5,100),_fScroll1,3,0,20);
        GUI.Label( Rect( 140 ,20, 150, 150),
                _sDescription);
        //GUILayout.Label(Rect(140,20,150,150),_sDescription);
        if(IsScrollShow(_sDescription ,150 ,150))
        {
                _fScroll2 = GUI.VerticalScrollbar(Rect(280,10,10,100),_fScroll2,3,0,20);
                //print("true");
        }
        else
        {
                //print("false");
        }
}

function IsScrollShow(sCount : String , iwidth : int , iheight : int)
{
        if(sCount.Length/iwidth * 100> iheight)
        {
                return true;
        }
        else
        {
                return false;
        }
}

 

在场景中新建一个GAMEOBJECT,把脚本拖到GAMEOBJECT上。
新建一个GUISKIN,GUISKIN中的FONT属性设置为中文字体,添加字体后下拉有选择的。

选中GAMEOBJECT,脚本中有个GUISKIN属性,下拉选择刚刚建立的GUISKIN

运行,动态加载了列表。改变XML文件,重新运行的话,数据会相应变化。

uke
User offline. Last seen 3 天 7 小时 ago. Offline
超级管理员
Joined: 09/07/2009
Points: 2499

偶是新手,不是老师!

cartoonboy
User offline. Last seen 51 周 3 天 ago. Offline
版主
Joined: 05/10/2010
Points: 316

谢谢uke老师的夸奖!

uke
User offline. Last seen 3 天 7 小时 ago. Offline
超级管理员
Joined: 09/07/2009
Points: 2499

cartoonboy朋友绝对是一好学的同学,总看到他提问题,而且都还是蛮有水准的问题,可惜我都没怎么研究过!