@mc , According my test, I reproduced your problem when I want to deserialize the xml to class object.
Based on my research, I find that we need to add namespace in the class define to solve the problem.
Here is a code example you could refer to.
Class code:
[XmlRoot(ElementName = "identifier")]
public class Identifier
{
[XmlAttribute(AttributeName = "id")]
public string Id { get; set; }
[XmlText]
public string Text { get; set; }
}
[XmlRoot(ElementName = "Meta")]
public class Meta
{
[XmlAttribute(AttributeName = "name")]
public string Name { get; set; }
[XmlAttribute(AttributeName = "content")]
public string Content { get; set; }
}
[XmlRoot(ElementName = "metadata")]
public class Metadata
{
[XmlElement(ElementName = "identifier",Namespace = "http://purl.org/dc/elements/1.1/")]
public Identifier Identifier { get; set; }
[XmlElement(ElementName = "date", Namespace = "http://purl.org/dc/elements/1.1/")]
public int Date { get; set; }
[XmlElement(ElementName = "language", Namespace = "http://purl.org/dc/elements/1.1/")]
public string Language { get; set; }
[XmlElement(ElementName = "meta")]
public Meta Meta { get; set; }
}
[XmlRoot(ElementName = "package",Namespace = "http://www.idpf.org/2007/opf")]
public class Package
{
[XmlElement(ElementName = "metadata")]
public Metadata Metadata { get; set; }
[XmlAttribute(AttributeName = "version")]
public double Version { get; set; }
[XmlAttribute(AttributeName = "unique-identifier")]
public string UniqueIdentifier { get; set; }
}
How to deserialize the xml to class:
XmlSerializer serializer = new XmlSerializer(typeof(Package));
string xml = File.ReadAllText("D:\\test.xml");
using (StringReader reader = new StringReader(xml))
{
var test = (Package)serializer.Deserialize(reader);
}
Result:
Best Regards,
Jack
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.