I am creating an API wrapper which gets an API response and converts it to POCO objects. Right now I have a test that follows this sequence of actions:
- Create object with XML element names and values.
- Serialize this object to XML format.
- Convert the XML tree back to an actual object trough my converter.
- Assert if the values inside my new object are as expected.
I have the following helper class which represents a show object:
class TestShow {
public string id { get; set; }
public string IMDB_ID { get; set; }
public string Language { get; set; }
}
The serializer:
var serializer = new XmlSerializer(show.GetType());
string xml;
using (var writer = new StringWriter()) {
serializer.Serialize(writer, show);
xml = writer.ToString();
}
However, when I serialize this to XML I get this result:
<?xml version="1.0" encoding="utf-16"?>
<TestShow xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<id>76290</id>
<IMDB_ID>tt0285331</IMDB_ID>
<Language>en</Language>
</TestShow>
The desired result is this:
<Data>
<Series>
<id>76290</id>
<IMDB_ID>tt0285331</IMDB_ID>
<Language>en</Language>
</Series>
</Data>
This shows two issues:
- The name of
TestShowshould be changed toSeries. Is there an easy way to do this, or should I just change my class name? - There is an upper collection called
Data. How would I add this?
XmlSerializer.