- Home /
Xml Serialization? (how to read/write?)
I finally decided to stop being scared of xml and just learn it already and stop avoiding it. And now I'm so confused. I've been through dozens of google/forums/youtube searches and I can not understand this XML serialization. Its all alien language to me.
First it took me a bit to realize that there's a few ways to go about reading and writing in xml files. There's that XmlDocument thing, and theres the serialization, and probably something else too. If I understood it correctly, those two things are different ways, and serializing is better. I sort of understood the document way, but serialization is confusing me even more.
Now I've been to the unify community tutorial about it and I don't get it. How exactly are you reading or writing anything? I just don't see it happening in the code. And why do I need more than one script? I can't find a simple example at all on how to do this. Or I'm just too stupid for this.
I wanted to learn by making a simple checklist program, and saving/loading the data from an xml file.
Here's the short version:
<Checklists>
<Checklist_1>
<Day_1>
<Thing_1>example 1</Thing_1>
<Thing_2>example 2</Thing_2>
</Day_1>
<Day_2>
<Thing_1>example 1</Thing_1>
<Thing_2>example 2</Thing_2>
</Day_2>
</Checklist_1>
<Checklist_2>
<Day_1>
<Thing_1>example 1</Thing_1>
<Thing_2>example 2</Thing_2>
</Day_1>
<Day_2>
<Thing_1>example 1</Thing_1>
<Thing_2>example 2</Thing_2>
</Day_2>
</Checklist_2>
</Checklists>
Now what to do in actual code? It got confusing real fast because this turns out to be an array within an array within an array.
This is as far as I got:
[XmlRoot("Checklists")]
public class Checklists
{
[XmlArray("Checklist_1")]
[XmlArrayItem("Day_1")]
public List<Day_1> Checklist_1 = new List<Day_1>();
}
public class Day_1
{
public string Thing_1;
public string Thing_2;
}
Yeah I know... its sad. Now if this is correct at all, I don't understand the reading and writing part in the tutorial. I don't see any variables like health being assigned or read there. Is it because of the [XmlRoot("Checklists")]
part? The vars and the xml file are being connected? And then the read write is just syncing everything from and to? What if you just wanted to sync one var? And how is then the public class Day_1 being connected with the file? Because its being declared in Checklists class? Or am I completely off on this?
Thank you.
Answer by Elenesski · Mar 20, 2015 at 08:03 PM
You could use an XDocument, this is a LINQ enabled XML parser to read XML documents or fragments. I'd really recommend that you not number the elements in your XML, as the order is implied by the sequence of elements in your XML.
StringReader SR = new StringReader(File.ReadAllText(XMLFile)); // or just some text text
XDocument DOC = XDocument.Load(SR);
foreach (XElement NODE in DOC.Nodes().OfType<XElement>().Where(A => A.Name == "Checklists")) {
foreach (XElement CHECKLIST in NODE.OfType<XElement>().Where(A => A.Name.ToString().StartsWith() == "CheckList_" ) ) {
// and so on
}
}
Call me crazy, but for writing, I tend to actually write my XML out with a StringBuilder and construct the XML on the fly. I found that the XML document libraries tended to be slower and gave me a bunch of other things that weren't necessary for most of my purposes.
I also recommend creating XElement extensions, to wrap things like the Where clauses, because these statements get to be really hard to read.
public static IEnumerable<XElement> GetAll(this XElement aNode, string aName) {
return aNode.Nodes().OfType<XElement>().Where(A => A.Name == aName );
}
and
public static IEnumerable<XElement> StartsWith(this XElement aNode, string aName) {
return aNode.Nodes().OfType<XElement>().Where(A => A.Name.ToString().StartsWith(aName));
}
So that you can code things like
foreach (XElement CHECKLIST in NODE.StartsWith("CheckList_") { ... }
To make it much easier to read and understand.
Your answer
Follow this Question
Related Questions
Read AND Write to XML at runtime 1 Answer
Loading Xml file issues 0 Answers
Reading level specific file in build (from another level) 0 Answers
Problems with the XML Serializer 1 Answer