- Home /
Problem with XML parse
I am saving my game dat to xml files and i have problem with one code that throws this error XmlException: Text node cannot appear in this state. Line 1, position 1. Mono.Xml2.XmlTextReader.ReadText (Boolean notWhitespace) Mono.Xml2.XmlTextReader.ReadContent () Mono.Xml2.XmlTextReader.Read () System.Xml.XmlTextReader.Read () Mono.Xml.XmlFilterReader.Read () System.Xml.Linq.XDocument.ReadContent (System.Xml.XmlReader reader, LoadOptions options) System.Xml.Linq.XDocument.LoadCore (System.Xml.XmlReader reader, LoadOptions options) System.Xml.Linq.XDocument.Load (System.IO.TextReader reader, LoadOptions options) System.Xml.Linq.XDocument.Parse (System.String s, LoadOptions options) System.Xml.Linq.XDocument.Parse (System.String s) SaveAndLoadSystem.LoadChunkState () (at Assets/scripts/SaveAndLoadSystem/SaveAndLoadSystem.cs:155) GameManager.OnLevelWasLoaded () (at Assets/scripts/GameManager.cs:93) when i try to parse file. The code is:
public void SaveChunkState(){
if (!System.IO.Directory.Exists(path + "/Chunks")) {
System.IO.Directory.CreateDirectory(path + "/Chunks");
}
if (File.Exists (path + "/Chunks/X" + manager.playerPosition.x.ToString() + "Y" + manager.playerPosition.y.ToString() +".xml")) {
File.Delete (path + "/Chunks/X" + manager.playerPosition.x.ToString () + "Y" + manager.playerPosition.y.ToString () + ".xml");
}
XElement save = new XElement ("save");
XElement itemsOnGround = new XElement ("itemsOnGround");
ItemOnGround[] items = GameObject.FindObjectsOfType<ItemOnGround> ();
foreach (ItemOnGround item in items) {
XAttribute name = new XAttribute ("name", item.itemName);
XAttribute posX = new XAttribute ("posX", item.gameObject.transform.position.x.ToString ());
XAttribute posY = new XAttribute ("posY", item.gameObject.transform.position.y.ToString ());
XAttribute posZ = new XAttribute ("posZ", item.gameObject.transform.position.z.ToString ());
XAttribute currentFill = new XAttribute ("currentFill", item.currentFill.ToString ());
XAttribute state = new XAttribute ("state", item.state.ToString ());
XAttribute stack = new XAttribute ("stack", item.stack.ToString ());
XElement itemm = new XElement("item", name, posX, posY, posZ, currentFill, state, stack);
itemsOnGround.Add (itemm);
}
save.Add (itemsOnGround);
LootBox[] lootBoxes = GameObject.FindObjectsOfType<LootBox> ();
XElement allLootBoxes = new XElement ("allLootboxes");
foreach (LootBox lootBox in lootBoxes) {
XElement lootBoxElement = new XElement("lootBox");
XElement lootBoxName = new XElement ("name", lootBox.lootBoxName);
lootBoxElement.Add (lootBoxName);
XAttribute posX = new XAttribute ("posX", lootBox.gameObject.transform.position.x.ToString ());
XAttribute posY = new XAttribute ("posY", lootBox.gameObject.transform.position.y.ToString ());
XAttribute posZ = new XAttribute ("posZ", lootBox.gameObject.transform.position.z.ToString ());
XElement lootBoxPosition = new XElement ("lootBoxPosition", posX, posY, posZ);
lootBoxElement.Add (lootBoxPosition);
List<ItemInInventory> itemsInLootBox = lootBox.loot;
XElement lootBoxContainment = new XElement ("lootBoxContainment");
foreach (ItemInInventory item in itemsInLootBox) {
XAttribute name = new XAttribute ("itemName", item.itemName);
XAttribute stac = new XAttribute ("stack", item.stack.ToString());
XAttribute state = new XAttribute ("state", item.state.ToString ());
XAttribute currentFill = new XAttribute ("currentFill", item.currentFill.ToString ());
XElement lootItem = new XElement("itemInLootBox", name, stac, state, currentFill);
lootBoxContainment.Add (lootItem);
}
lootBoxElement.Add (lootBoxContainment);
allLootBoxes.Add (lootBoxElement);
}
save.Add (allLootBoxes);
ResourcesSource[] resources = GameObject.FindObjectsOfType<ResourcesSource>();
XElement allResources = new XElement ("allResources");
for (int i = 0; i < resources.Length; i++) {
XAttribute posX = new XAttribute ("posX", resources[i].gameObject.transform.position.x.ToString());
XAttribute posY = new XAttribute ("posY", resources[i].gameObject.transform.position.y.ToString());
XAttribute posZ = new XAttribute ("posZ", resources[i].gameObject.transform.position.z.ToString());
XAttribute rotY = new XAttribute ("rotY", resources [i].gameObject.transform.rotation.eulerAngles.y.ToString ());
XElement resourceTransform = new XElement ("lootBoxPosition", posX, posY, posZ, rotY);
XElement resourceLeft = new XElement ("resourceLeft", resources [i].resourceAmount.ToString ());
XElement resourceName = new XElement ("resourceName", resources [i].gameObject.name);
XElement resourceSource = new XElement ("resourceSource", resourceTransform, resourceLeft, resourceName);
allResources.Add (resourceSource);
}
save.Add (allResources);
File.WriteAllText (path + "/Chunks/X" + manager.playerPosition.x.ToString () + "Y" + manager.playerPosition.y.ToString () + ".xml", save.ToString ());
}
public void LoadChunkState(){
ItemsDatabase database = GameObject.FindObjectOfType<ItemsDatabase> ();
database.InitiateDatabase ();
if (File.Exists (path + "/Chunks/X" + manager.playerPosition.x.ToString () + "Y" + manager.playerPosition.y.ToString () + ".xml")) {
XElement save = XDocument.Parse (path + "/Chunks/X" + manager.playerPosition.x.ToString () + "Y" + manager.playerPosition.y.ToString () + ".xml").Element ("save");
foreach(XElement itemEl in save.Element("itemsOnGround").Elements("item")){
string itemName = itemEl.Attribute ("name").Value;
float posX = float.Parse (itemEl.Attribute ("posX").Value);
float posY = float.Parse (itemEl.Attribute ("posY").Value);
float posZ = float.Parse (itemEl.Attribute ("posZ").Value);
float currentFill = float.Parse (itemEl.Attribute ("currentFill").Value);
float state = float.Parse (itemEl.Attribute ("state").Value);
int stack = int.Parse(itemEl.Attribute ("stack").Value);
GameObject item = Instantiate (Resources.Load<GameObject> ("Items/" + itemName), new Vector3 (posX, posY, posZ), Quaternion.identity);
ItemOnGround itemOnGround = item.GetComponent<ItemOnGround> ();
itemOnGround.currentFill = currentFill;
itemOnGround.state = state;
itemOnGround.stack = stack;
}
foreach (XElement lootBoxEl in save.Element("allLootBoxes").Elements("lootBox")) {
string lootBoxName = lootBoxEl.Element ("name").Value;
float posX = float.Parse (lootBoxEl.Attribute ("posX").Value);
float posY = float.Parse (lootBoxEl.Attribute ("posY").Value);
float posZ = float.Parse (lootBoxEl.Attribute ("posZ").Value);
GameObject lootBox = Instantiate (Resources.Load<GameObject> ("LootBoxes/" + lootBoxName), new Vector3 (posX, posY, posZ), Quaternion.identity);
foreach (XElement item in lootBoxEl.Element("lootBoxContainment").Elements("itemInLootBox")) {
string itemName = item.Attribute ("itemName").Value;
int stack = int.Parse (item.Attribute ("stack").Value);
float state = float.Parse (item.Attribute ("state").Value);
float currentFill = float.Parse (item.Attribute ("currentFill").Value);
LootBox lootBoxCont = lootBox.GetComponent<LootBox> ();
ItemInInventory addedItem = database.items [itemName].MakeCopy();
addedItem.state = state;
addedItem.currentFill = currentFill;
addedItem.stack = stack;
addedItem.isEquiped = false;
lootBoxCont.loot.Add (addedItem);
}
}
foreach (XElement resourceSource in save.Element("allResources").Elements("resourceSource")) {
Transform resourceSpawner = GameObject.Find ("ResourcesSpawner").transform;
string resourceName = resourceSource.Element ("resourceName").Value;
float posX = float.Parse (resourceSource.Element("resourceTransform").Attribute ("posX").Value);
float posY = float.Parse (resourceSource.Element("resourceTransform").Attribute ("posY").Value);
float posZ = float.Parse (resourceSource.Element("resourceTransform").Attribute ("posZ").Value);
float rotY = float.Parse(resourceSource.Element("resourceTransform").Attribute ("rotY").Value);
Quaternion resourceRotation = new Quaternion ();
resourceRotation.eulerAngles = new Vector3 (0f, rotY, 0f);
GameObject resource = Instantiate (Resources.Load<GameObject> ("/ResourceSources" + resourceName), new Vector3 (posX, posY, posZ), resourceRotation, resourceSpawner);
ResourcesSource resSource = resource.GetComponent<ResourcesSource> ();
resSource.resourceAmount = int.Parse (resourceSource.Attribute ("resourceLeft").Value);
}
}
}
And it is not working. BUT i am saving other data EXACTLY the same way and it works fine and is beeing loaded fine. What the problem here?
Answer by Bunny83 · Oct 27, 2017 at 12:28 PM
XDocument.Parse expects an XML string, not a path name. (Line 69 in your posted code).
You might want to use XDocument.Load which does take a path
Your answer
Follow this Question
Related Questions
i am pretty sure that this is right but..... 1 Answer
Parsing error 1 Answer
Parseing Varible names from strings 1 Answer
Retrive value from xml 1 Answer
Parse xml with unknown fields 1 Answer