I have a timer application that I am working on that uses two separate XML files to store data. XML File 1 stores summary data for the time allotted and the name of the game. XML File 2 stores the contents of a dataset which is more of a narrative of events. This allows the user to load saved data and basically start from where they left off. I am using serialize/deserialize for XML File 1 and loading the values into variables and dataset.XMLReader(xml file path) to load the dataset.
I would like to combine the two XML files, but after a couple of hours of searching around and trying bits of different code I have hit an impasse. Is what I am attempting to do even possible?
One of my thoughts was instead of using serialization/deserialization for XML File 1, retool my application (ugh) to load these variables into a dataset as well, then populate the variables from there. But then I still have the problem with dataset.XMLReader(xml file path) running into issues with either multiple root nodes or inconsistent node structures.
Questions, comments or holy grail solution that makes my life easier in 3 lines of code much appreciated.
Code for XML File 1:
static public void SerializeToXML(TimeData times, String filename)
{
XmlSerializer serializer = new XmlSerializer(typeof(TimeData));
TextWriter textWriter = new StreamWriter(filename);
serializer.Serialize(textWriter, times);
textWriter.Close();
}
private void DeSerializeFromXML(string filename)
{
XmlSerializer serializer = new
XmlSerializer(typeof(TimeData));
FileStream fs = new FileStream(filename, FileMode.Open);
XmlReader reader = XmlReader.Create(fs);
TimeData i;
i = (TimeData)serializer.Deserialize(reader);
[bunch of code to convert ticks to readable time values and whatnot]
fs.Close();
}
XML File 1:
<?xml version="1.0" encoding="utf-8"?>
<TimeData>
<Name>Test</Name>
<TimeAllotted>76000000</TimeAllotted>
<CumulativeTime>0</CumulativeTime>
<TimeRemaining>76000000</TimeRemaining>
</TimeData>
Code for XML File 2:
Essentially it is just ds.ReadXml(xmlFileName); and ds.WriteXml(newFilename);
XML File 2:
<?xml version="1.0" encoding="utf-8"?>
<Detail>
<Timer>
<ID>1</ID>
<Segment>1</Segment>
<Event>Start</Event>
<Value>00:00:00</Value>
<Notes>Some Text</Notes>
</Timer>
<Timer>
<ID>2</ID>
<Segment>1</Segment>
<Event>Start</Event>
<Value>00:00:00</Value>
<Notes>Some More Text</Notes>
</Timer>
</Detail>
What I would like the resulting XML file to look like, or at least something similar containing all the data from above:
XML File 1:
<?xml version="1.0" encoding="utf-8"?>
<TimeApp>
<TimeData>
<Name>Test</Name>
<TimeAllotted>76000000</TimeAllotted>
<CumulativeTime>0</CumulativeTime>
<TimeRemaining>76000000</TimeRemaining>
</TimeData>
<Detail>
<Timer>
<ID>1</ID>
<Segment>1</Segment>
<Event>Start</Event>
<Value>00:00:00</Value>
<Notes>Some Text</Notes>
</Timer>
<Timer>
<ID>2</ID>
<Segment>1</Segment>
<Event>Stop</Event>
<Value>00:00:00</Value>
<Notes>Some More Text</Notes>
</Timer>
</Detail>
</TimeApp>