If there is a better forum or newsgroup, please feel to point out where to ask…
I’m an experienced Python programmer, so I’m feeling a little defensive that C# Unity is confusing me a bit here…
I am attempting to open a XML file, from a web server, that is digest authentication (so I am using Best HTTP to support Digest authentication, instead of www, which doesn’t support Digest). Then I need to parse the XML, to scrap some data out of it.
I am doing fine, until I go to parse the XML, which I can’t tell if I am passing the data poorly, or if I am missing some subtle C# error, that is killing the data.
Plus, the Print / Debug statement’s don’t seem to be outputting the data to the Unity console?
Can anyone point me in the right direction?
- Ben
using UnityEngine;
using System.Collections;
using System.Xml;
using System.IO;
using System.Collections.Generic;
using System;
using BestHTTP;
using BestHTTP.Authentication;
//using BestHTTP.Caching;
public class xmlLoader : MonoBehaviour
{
public string xmlPath = "";
public string Username = "";
public string Password = "";
public string xmlContent = "";
void Start ()
{
Debug.Log ("Started");
WebObject (xmlPath);
}
void WebObject (string param)
{
Debug.Log ("Downloading Device List");
xmlPath = param;
var request = new HTTPRequest (new Uri (xmlPath), OnFinishedDeviceDownload);
request.Credentials = new Credentials (Username, Password);
request.DisableCache = true;
request.Send ();
}
void OnFinishedDeviceDownload (HTTPRequest request, HTTPResponse response)
{
// Check for errors:
if (response == null) {
Debug.LogError ("No response received: " + request.Exception.ToString () + "\n\r" + request.Exception.Message + "\n\r" + request.Exception.StackTrace);
return;
}
xmlContent = response.DataAsText;
Debug.Log ("Done Downloading Device List");
ParseDevices_XML (xmlContent);
}
//
// XML Structured as below
//
// <devices>
// <device href="/devices/Activate%20Hallway%20Lights.xml">Activate Hallway Lights</device>
// <device href="/devices/Basement%20Door.xml">Basement Door</device>
// <device href="/devices/Basement%20Lights%20%232.xml">Basement Lights #2</device>
// <device href="/devices/Basement%20Water.xml">Basement Water</device>
// </Devices>
private void ParseDevices_XML (string xmlData)
{
Debug.Log ("In ParseDevices_XML");
XmlDocument xmlDataStore = new XmlDocument ();
xmlDataStore.Load (new StringReader (xmlContent));//(xmlData));
string xmlPathPattern = "//devices/device";
XmlNodeList myNodeList = xmlDataStore.SelectNodes (xmlPathPattern);
foreach (XmlNode node in myNodeList) {
Debug.Log ("Processing Node");
print (DeviceRecordString (node));
}
}
private string DeviceRecordString (XmlNode node)
{
Debug.Log ("In DeviceRecordString");
XmlNode DeviceURL = node.FirstChild;
XmlNode DeviceName = node.NextSibling;
return DeviceName + " " + DeviceURL;
}
private void ParseDevices_BruteForce (string xmlData)
{
string[] workingdata = xmlData.Split ('<');
Debug.Log (workingdata);
}
}