XML Deserialization : from xmlString to complex class



This is the xml what I get from database:




> <Library>
> <Header>
> <HeaderID>01</HeaderID>
> </Header>
> <Books>
> <Book>
> <Name>Book1</Name>
> <Category>Fiction</Category>
> <Authors>2</Authors>
> </Book>
> <Book>
> <Name>Book2</Name>
> <Category>Non - Fiction</Category>
> <Authors>1</Authors>
> </Book>
> </Books>
> <Authors>
> <Author>
> <Name>Author1ForBook1</Name>
> </Author>
> <Author>
> <Name>Author2ForBook1</Name>
> </Author>
> <Author>
> <Name>Author1ForBook2</Name>
> </Author>
> </Authors>
></Library>


This is my class:



public class Library
{
public Header header{get; set;}
public List<Book> books{get;set;}
public List<Author> authors{get;set;}
}

public class Header
{
int ID{get;set;}
}

public class Book
{
string Name{get; set;}
string Category{get; set;}
string Fiction{get; set;}
}

public class Author
{
string Name{get;set;}
}


What I want to be able to do is, to deserialize the xml string I get from database into the class object - Library. Currently I am using the following code to it, but I get an error:



String xData = RequestXml;
XmlSerializer x = new XmlSerializer(typeof(Library));
Library myTest = (Library)x.Deserialize(new StringReader(xData));


The error I get is:


There is an error in XML document (1, 2).


Please help me resolve this issue.


No comments:

Post a Comment