I am dealing with an API that occasionally changes the namespaces on the XML that I receive. The XML structure remains the same. I need to deserialize the XML into a strongly typed model.
How do I perform the deserialization regardless of what namespace is on the XML?
I was using a model like this:
[Serializable, XmlRoot(ElementName = "TestModel", Namespace = "http://ift.tt/1B24abW")]
public class TestModel
{
public TestModel()
{
TestElements = new List<TestModelChildren>();
}
[XmlElement("TestModelChildren")]
public List<TestModelChildren> TestElements { get; set; }
}
I try to deserialize some XML into that model with code like this:
public TestModel DeserializeIt(XDocument xDoc)
{
TestModel result;
var serializer = new XmlSerializer(typeof(TestModel));
using(var sr = new StringReader(xDoc.ToString()))
{
result = (TestModel)serializer.Deserialize(sr);
}
return result;
}
My problem is that every so often, the namespace on the XML I am receiving changes. I might start getting XML like this:
<TestModel xmlns:i="http://ift.tt/ra1lAU" xmlns="http://ift.tt/1D4bTLs">
<TestModelChildren>
...
</TestModelChildren>
</TestModel>
I don't want to have to recompile my code every time this namespace change happens. How do I deal with it?
No comments:
Post a Comment