Need Beginner C# help?

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);
		}
	
	
}

When you run it… what is the last Debug statement that is actually successfully output to the console?

Well, that might be part of the confusion…

The last entry in the console is:

System.Xml.XmlText
UnityEngine.MonoBehaviour:print(Object)
xmlLoader:ParseDevices_XML(String) (at Assets/xmlLoader.cs:68)
xmlLoader:OnFinishedDeviceDownload(HTTPRequest, HTTPResponse) (at Assets/xmlLoader.cs:45)
BestHTTP.HTTPRequest:CallCallback()
BestHTTP.HTTPConnection:HandleCallback()
BestHTTP.HTTPManager:OnUpdate()
BestHTTP.HTTPUpdateDelegator:LateUpdate()


The going back two entries (n-3) is a good candidate for the data output, but I'm not seeing real data:

In DeviceRecordString
UnityEngine.Debug:Log(Object)
xmlLoader:smile:eviceRecordString(XmlNode) (at Assets/xmlLoader.cs:74)
xmlLoader:ParseDevices_XML(String) (at Assets/xmlLoader.cs:68)
xmlLoader:OnFinishedDeviceDownload(HTTPRequest, HTTPResponse) (at Assets/xmlLoader.cs:45)
BestHTTP.HTTPRequest:CallCallback()
BestHTTP.HTTPConnection:HandleCallback()
BestHTTP.HTTPManager:OnUpdate()
BestHTTP.HTTPUpdateDelegator:LateUpdate()

Ok… that looks like part of a stack trace… not sure why it’s only part of one… but see how it’s showing the line numbers there? Line 68 and line 45… So there’s an error happening somewhere… you may have to set some breakpoints and see what’s going on. If you go back a little further… looks again like an exception but not sure what it was… Can you run this again and then just paste the entire console log around this? I’m guessing that since this is an async request you’re missing part of what’s going on and it’s not all being output to the console.

Silly question, in Unity3d, I am unable to highlight the entire log, it is breaking it in chunks. So, one obvious answer is to open the console app and manually find the log file. But is there a better way to do this from inside Unity3d?

Hmm… I’m sure there’s a way but I couldn’t tell you offhand. One thing you can try is wrapping your bits of code in Try / Catch blocks and output to console.

try
{
   //Your code here
}
catch(Exception ex)
{
     Debug.Log(ex.ToString());
}

Do that in each of your methods… that should output it as one block.