- Home /
Need help in appending to Xml document
hello everyone,
I've been having a lot of difficulty in getting this to work. I've scoured the internet for a solution, but couldn't find any.
Here is my problem:
This is the XML document that I need to append to.
<?xml version="1.0" encoding="utf-8"?>
<Participants>
<Participant>
<Price>175000</Price>
<Choice>Neither</Choice>
<Choice>1</Choice>
<Choice>Neither</Choice>
<Municipality>NDG</Municipality>
<HouseholdSize>4</HouseholdSize>
<Gender>Female</Gender>
<Age>55-64</Age>
<Employment>student and employed</Employment>
<Income>75000-99999</Income>
</Participant>
<Participant>
<Price>225000</Price>
</Participant>
</Participants>
I need to append to the second Participant and I have the following code to do so.
if(File.Exists("Information.xml") && PlayerPrefs.GetInt("Participant size")>1)
{
var tracker=PlayerPrefs.GetInt("Participant size");
xmlRead.WhitespaceHandling=WhitespaceHandling.None;
xmlRead.MoveToContent();
xmlRead.Read();
for(var i=1;i<tracker;i++)
{
xmlRead.Skip();
xmlRead.Skip();
}
doc.Load(xmlRead);
var elem =doc.CreateElement("Choice");
elem.InnerText=Choice();
doc.DocumentElement.AppendChild(elem);
xmlRead.Close();
doc.Save("Information.xml");
}
However whenever it gets to Doc.Load it gives me the following error:
NullReferenceException: Unexpected node type EndElement. System.Xml.XmlDocument.ReadNodeCore (System.Xml.XmlReader reader) System.Xml.XmlDocument.ReadNode (System.Xml.XmlReader reader) System.Xml.XmlDocument.Load (System.Xml.XmlReader xmlReader) selectNeighborhood.OnGUI () (at Assets/scripts/GUI Scripts/selectNeighborhood.js:145)
Does anyone know why?
Additional info if needed:
xmlRead is defined as:
var readerSettings = new XmlReaderSettings();
readerSettings.IgnoreWhitespace = true;
readerSettings.IgnoreComments = true;
var xmlRead= XmlReader.Create("Information.xml",readerSettings);
Thanks guys and gals,
Answer by Holymolymo · Mar 20, 2012 at 10:45 PM
Hi Dave A.
I gave up taking that approach and went in another direction which ended up being a better solution and I was able to get rid of a lot of redundant code.
The Skip() was being used to place itself to the last Participant. In this case the second participant. After extensive testing I discovered that it was the only way of placing the reader in the right place if that makes any sense.
Do know that it was my first time using Xml reader, writer and document ,so in hindsight it probably was a good idea to redo it after experimenting for a bit.
Thanks anyways!
You should use the 'comment' feature to respond to specific answers. Glad to hear it's working.
Answer by DaveA · Mar 20, 2012 at 07:27 PM
Several thoughts:
You don't need to parse and build XML with XmlReader to do this. You could read the file to the tag and use string manipulation (for example) to insert what you need.
What is intended here:
for(var i=1;i<tracker;i++)
{
xmlRead.Skip();
xmlRead.Skip();
}
If that's supposed to skip the second Participant, why two skips, why start a 1 (not 0)?
You could also use doc.FirstChild then NextSibling to skip Participants. In that case, read the whole doc (don't bother with Skip)
Your answer
Follow this Question
Related Questions
"Root Element Missing: XML Exception" when trying to load data from XML file 2 Answers
Serialize a class that extends as a serializable class? 0 Answers
XML Serialization issues 0 Answers
loading XML quiz 1 Answer
XML Serialization not working on WebGL 0 Answers