I am currently trying to deserialize an XML using the [Serializable()] command object and I am having some issue. Here is the code sample :
using System;
using System.Xml.Linq;
using System.Xml.Serialization;
using System.IO;
namespace XmlManipulation
{
public class XmlManip
{
public static TestCasesList SerializeTest() //test function
{
TestCasesList testslist = null;
string path = @"C:\Users\sxtx2987\Documents\Visual Studio 2015\Projects\FDB\deleteme\test.xml";
XmlSerializer serializer = new XmlSerializer(typeof(TestCasesList));
StreamReader reader = new StreamReader(path);
testslist = (TestCasesList)serializer.Deserialize(reader);
reader.Close();
return testslist;
}
}
[Serializable()]
public class TestCase
{
[System.Xml.Serialization.XmlElement("name")]
public string name;
[System.Xml.Serialization.XmlElement("objective")]
public string objective;
[System.Xml.Serialization.XmlElement("criteria")]
public string criteria;
[System.Xml.Serialization.XmlElement("issue")]
public string issue;
[System.Xml.Serialization.XmlElement("clientcomments")]
public string clientcomments;
[System.Xml.Serialization.XmlElement("authoritycomments")]
public string authoritycomments;
}
[Serializable()]
[XmlType("testcaseslist")]
public class TestCasesList
{
[XmlArray("testcase")]
public TestCase[] TestCase { get; set; }
}
}
And here is the file I am trying to deserialize
<xml version="1.00" encoding="UTF-8">
<campaign>
<name>Test XML</name>
<number>XXXXXG04-06-1</number>
<client>Fictional</client>
<model>Rocket powered boots</model>
<authority>Fictional authority</authority>
</campaign>
<testcaseslist>
<testcase>
<name>TestCase001</name>
<objective>Objective test 1</objective>
<criteria>Pass criteria test 1</criteria>
<issue>Issue Description test 1</issue>
<clientcomments>Client comments test 1</clientcomments>
<authoritycomments>Authority comments test 1</authoritycomments>
</testcase>
<testcase>
<name>TestCase002</name>
<objective>Objective test 2</objective>
<criteria>Pass criteria test 2</criteria>
<issue>Issue Description test 2</issue>
<clientcomments>Client comments test 2</clientcomments>
<authoritycomments>Authority comments test 2</authoritycomments>
</testcase>
</testcaseslist>
</xml>
What I am trying to achieve is taking all the roots element "testcase" and put each child element (namely : name,objective,criteria,issue,clientcomments,authoritycomments) in an class TestCasesList made of multiple classes TestCase.
So in this example :
TestCasesList
|-> testcase 1
|-> testcase 2
|-> etc....
which contains all the elements of the test case.
The code I have above is what I could gather up to now, but since this is quite a specific action that I am trying to do, I fail to find all the relevant information to solve my problem.
The code does not throw any errors, but when I try to call the function
TestCasesList test = null;
test = XmlManipulation.XmlManip.SerializeTest();
I get an empty test object.
I think that my problem is the XmlType and XmlArray section, but after a few hours of research and trials, I still cannot get the information from the XML.
Thank you for your time.