2 hiramyoucan hiramyoucan 于 2013.02.05 15:51 提问

Unity3D 实时解析XML时报错

获取网络时间:

IEnumerator GetTime()

    {
        WWW www = new WWW(timeURL);
        while (!www.isDone)
        {
            yield return www;
            Debug.Log("Doneeeeeeeeeeeeee" + www.text);
            ParseXml(www);
        }
    }`

后得到:`

<?xml version="1.0" encoding="GB2312" ?> 
            - <ntsc>
            - <time>
              <year>2011</year> 
              <month>7</month> 
              <day>10</day> 
              <Weekday /> 
              <hour>19</hour> 
              <minite>45</minite> 
              <second>37</second> 
              <Millisecond /> 
              </time>
              </ntsc>`

然后解析时报错:

public void ParseXml(WWW www)
{
    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.Load(www.text);
    XmlElement root = xmlDoc.DocumentElement;

    XmlNodeList nodeList = root.SelectNodes("/ntsc/time");
    foreach (XmlElement xe in nodeList)
    {
        foreach (XmlElement x1 in xe.ChildNodes)
        {
            if (x1.Name == "year")
            {
                Debug.LogError("x1.InnerText" + x1.InnerText);
            }
        }
    }
}

报错语句:xmlDoc.Load(www.text);
请问如何解决,谢谢!

2个回答

hiramtan
hiramtan   2013.04.18 18:04
已采纳

还是我自己回答吧:

public void ParseXml(WWW www)
{
    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.LoadXml(www.text);
    XmlElement root = xmlDoc.DocumentElement;

    XmlNodeList nodeList = root.SelectNodes("/ntsc/time");
    foreach (XmlElement xe in nodeList)
    {
        foreach (XmlElement x1 in xe.ChildNodes)
        {
            if (x1.Name == "year")
                Debug.Log("Current year:      " + x1.InnerText);
            if (x1.Name == "month")
                Debug.Log("Current month:      " + x1.InnerText);
            if (x1.Name == "day")
                Debug.Log("Current day:      " + x1.InnerText);
            if (x1.Name == "hour")
                Debug.Log("Current hour:      " + x1.InnerText);
            if (x1.Name == "minite")
                Debug.Log("Current minite:      " + x1.InnerText);
            if (x1.Name == "second")
                Debug.Log("Current second:      " + x1.InnerText);
        }
    }
}
AWNUXCVBN
AWNUXCVBN   2013.10.11 14:45

xmlDoc.LoadXml(www.text);

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!