I'm making a XML deserializer for a certain type of XML. I already have one question regarding it :-) Back then I was trying to conform the namespaces, I have in the sample file. But the truth is - I get XMLs from many different sources. Although they're basically the same XML, some of them follow different namespaces, or add redundant namespaces of their own. That's why I removed namespaces from code (inside XmlRoot Attributes) and used the following code to ignore namespaces:
private Jpk DeserializeInputFile(string path)
{
Jpk obj;
var fileContent = File.ReadAllText(path);
using (TextReader textReader = new StringReader(fileContent))
{
using (XmlTextReader reader = new XmlTextReader(textReader))
{
reader.Namespaces = false;
XmlSerializer serializer = new XmlSerializer(typeof(Jpk));
obj = (Jpk)serializer.Deserialize(reader);
}
}
return obj;
}
Now I encountered a different situation. I received an XML file that starts with the following root:
<tns:JPK xmlns:etd="http://crd.gov.pl/xml/schematy/dziedzinowe/mf/2018/08/24/eD/DefinicjeTypy/" xmlns:tns="http://jpk.mf.gov.pl/wzor/2019/09/27/09271/">
When trying to deserialize it I get the following exception:
System.InvalidOperationException: There is an error in XML document (1, 57). ---> System.InvalidOperationException: <tns:JPK xmlns=''> was not expected.
Previously there was no namespace in the root node. No tns:JPK, just JPK. How can I ignore this namespace in root node?