Given the following XMLs, how could I deserialize them to the same C# object? Version 1.1 is essentially backward compatible to 1.0 so I don't really care about the xmlns value but C# thinks otherwise...
Example version 1.0:
<?xml version="1.0"?>
<HelloWorld xmlns="https://www.hello.ca/world/1.0">
Example version 1.1:
<?xml version="1.0"?>
<HelloWorld xmlns="https://www.hello.ca/world/1.1">
However, even if I don't specify the namespace in my class, when calling Deserialize it would still complaint about it. The code:
[Serializable()]
// other stuff I tried before
//[XmlType(AnonymousType = true, Namespace = "https://www.hello.ca/world/1.1")]
//[XmlRoot(Namespace = "https://www.hello.ca/world/1.1", IsNullable = false)]
[System.ComponentModel.DesignerCategory("code")]
[XmlType("HelloWorld")]
public class HelloWorld
{
...
}
Then in the code where I tried to Deserialize it into my object:
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.PreserveWhitespace = true;
xmlDoc.Load("somewhere.xml");
using (XmlReader xr = new XmlNodeReader(xmlDoc))
{
XmlSerializer xs = new XmlSerializer(typeof(HelloWorld));
blah = xs.Deserialize(xr) as HelloWorld;
return blah != null;
}
The error I get every single time is that, it doesn't like the xmlns in the node.
InvalidOperationException
> There is an error in the XML document.
> <HelloWorld xmlns='https://www.hello.ca/world/1.0'> was not expected.
What is the correct way to tell XmlSerializer to just ignore the xmlns? Or perhaps makes it such that the class is compatible for multiple xmlns?
here i have created a small example to ignore namespaces hope this helps you.