Hi guys,
I’m trying to make a form to fill in in a Unity project that collects and sends data to a XML document online.
I managed to make a successful local file that saves absolutely fine, but once I try to hook it up to the internet I get an error and I’m not sure what’s going on. Can someone explain where I’m going wrong?
WORKING LOCAL VERSION
public void NameAndEmail()
{
string filepath = Application.dataPath + @"/DataSharing/CustomerData.xml";
XmlDocument xmlDoc = new XmlDocument();
if(File.Exists (filepath))
{
xmlDoc.Load(filepath);
XmlElement elmRoot = xmlDoc.DocumentElement;
elmRoot.RemoveAll();
// To refresh document.. Only for testing purposes.
XmlNodeList iEmployees = xmlDoc.GetElementsByTagName("Name");
int iNoEmployees = iEmployees.Count;
XmlElement employee = xmlDoc.CreateElement("Employee_No_" + iNoEmployees.ToString() );
if( !PlayerPrefs.HasKey("EmployeeNumber") )
{
PlayerPrefs.SetString("EmployeeNumber", "Employee_No_" + iNoEmployees.ToString());
}
else
{
employee = xmlDoc.CreateElement( PlayerPrefs.GetString("EmployeeNumber") );
}
XmlElement employeeName = xmlDoc.CreateElement("Name");
employeeName.InnerText = sName;
XmlElement employeeEmail = xmlDoc.CreateElement("Email");
employeeEmail.InnerText = sEmail;
employee.AppendChild(employeeName);
employee.AppendChild(employeeEmail);
elmRoot.AppendChild(employee);
xmlDoc.Save(filepath); // save file.
}
else
{
Debug.Log("XML Document Not Loaded Correctly");
}
LoadGame();
}
NOT WORKING ONLINE VERSION
IEnumerator NameAndEmail()
{
string url = "http://www.nigelclark.com/test/blank.xml";
WWW www = new WWW(url);
yield return www;
//XmlDocument xmlDoc = new XmlDocument();
if(www.error == null)
{
string xml = www.text;
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xml);
XmlElement elmRoot = xmlDoc.DocumentElement;
//elmRoot.RemoveAll();
// To refresh document.. Only for testing purposes.
XmlNodeList iEmployees = xmlDoc.GetElementsByTagName("Name");
int iNoEmployees = iEmployees.Count;
Debug.Log(iNoEmployees);
XmlElement employee = xmlDoc.CreateElement("Employee_No_" + iNoEmployees.ToString() );
if( !PlayerPrefs.HasKey("EmployeeNumber") )
{
PlayerPrefs.SetString("EmployeeNumber", "Employee_No_" + iNoEmployees.ToString());
}
else
{
employee = xmlDoc.CreateElement( PlayerPrefs.GetString("EmployeeNumber") );
}
XmlElement employeeName = xmlDoc.CreateElement("Name");
employeeName.InnerText = sName;
XmlElement employeeEmail = xmlDoc.CreateElement("Email");
employeeEmail.InnerText = sEmail;
employee.AppendChild(employeeName);
employee.AppendChild(employeeEmail);
elmRoot.AppendChild(employee);
xmlDoc.LoadXml(xml);
//xmlDoc.Save(xml);
//xmlDoc.Save(filepath); // save file.
}
else
{
Debug.Log("XML Document Not Loaded Correctly");
}
LoadGame();
yield return null;
}
I’m getting this error:
XmlException: Document element did not appear. Line 1, position 1.
Mono.Xml2.XmlTextReader.Read ()
System.Xml.XmlTextReader.Read ()
System.Xml.XmlDocument.ReadNodeCore (System.Xml.XmlReader reader)
System.Xml.XmlDocument.ReadNode (System.Xml.XmlReader reader)
System.Xml.XmlDocument.Load (System.Xml.XmlReader xmlReader)
System.Xml.XmlDocument.LoadXml (System.String xml)
XMLInformation+<NameAndEmail>c__Iterator7.MoveNext () (at Assets/Code/XMLInformation.cs:82)
But I’m not sure what I’m actually doing wrong?
Can anyone help?
Thanks!