how to deserialize a xml to object?

mc 4,516 Reputation points
2021-11-30T13:28:09.573+00:00
<?xml version="1.0" encoding="utf-8" standalone="no"?>

<package version="2.0" xmlns="http://www.idpf.org/2007/opf" unique-identifier="bookid">
<metadata xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:opf="http://www.idpf.org/2007/opf">
<dc:identifier id="bookid">easypub-ed863743</dc:identifier>
<dc:date>2019</dc:date>
<dc:language>zh-CN</dc:language>
<meta name="cover" content="cover-image"/>
</metadata>
</package>
</xml>

when I want to deserialize the xml it will give a error that package xmlns="http://www.idpf.org/2007/opf" is unexpected.

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,962 questions
{count} votes

Accepted answer
  1. Jack J Jun 24,506 Reputation points Microsoft Vendor
    2021-12-01T02:49:46.997+00:00

    @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:

    153859-image.png

    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.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Artemiy Moroz 271 Reputation points
    2021-11-30T17:23:47.787+00:00

    Hi there! I would advise using XDocument with XNamespace.
    Please look a the code sample: Parsing FB2 format book in C#.
    You can use the sample as a starting point to build your application.
    The approach uses the XDocument class to get the XElements. The trick with further parsing is to use the correct XNamespace. 


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.