I want to deserialize a XML node to a class, with all the subnodes values into class properties
I got this error
-
ex {"There is an error in XML document (1, 2)."} System.Exception {System.InvalidOperationException}
and the message of the exception
-
InnerException {"<LITERALS xmlns=''> was not expected."} // If i use InnerXml it would be REVISION xmlns...
I tried Outer and Inner XML with a lot of solutions that appear in stackoverflow. I dont know why it adds xmlns
My xml is like that:
<root>
<LITERALS>
<REVISION>Rev.</REVISION>
<SERIE>Serie</SERIE>
</LITERALS>
<MORENODES></MORENODES>
<MORENODES></MORENODES>
</root>
And my class Literals to deserialize the xml node
{
// I tried these options that i saw in a post
// [XmlRoot(ElementName="LITERALS")]
// [Serializable, XmlRoot("LITERALS")]
[XmlRoot(Namespace = "www.idk.com", ElementName = "LITERALS", DataType = "string", IsNullable = true)]
public class Literals
{
[XmlElement(ElementName = "REVISION")]
public string REVISION { get; set; }
[XmlElement(ElementName = "SERIE")]
public string SERIE { get; set; }
}
}
My code
XmlNode literalsNodo = courseNodo.SelectSingleNode("LITERALS");
XmlRootAttribute xRoot = new XmlRootAttribute("LITERALS");
xRoot.ElementName = "LITERALS";
xRoot.Namespace = "http://www.idk.com";
xRoot.IsNullable = true;
if (literalsNodo != null)
{
XmlSerializer xs = new XmlSerializer(typeof(Literals),xRoot);
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(literalsNodo.InnerXml));
literals = (Literals)xs.Deserialize(ms);
}