public class Book
{
[DataMember]
[XmlElement(ElementName = "ID")]
public string ID;
[DataMember]
[XmlElement(ElementName = "Title")]
public string Title;
[DataMember]
[XmlElement(ElementName = "Author")]
public string Author;
[DataMember]
[XmlElement(ElementName = "Description")]
public string Description;
[DataMember]
[XmlElement(ElementName = "Genre")]
public string Genre;
[DataMember]
[XmlElement(ElementName = "Price")]
public decimal Price;
[DataMember]
[XmlElement(ElementName = "PublishDate")]
public DateTime PublishDate;
}
}
The xml that the service is returning is from a xml file which looks like:-
<?xml version="1.0" encoding="utf-8"?>
<catalog >
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>
An in-depth look at creating applications
with XML.
</description>
</book>
<book id="bk102">
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-12-16</publish_date>
<description>
A former architect battles corporate zombies,
an evil sorceress, and her own childhood to become queen
of the world.
</description>
</book>
</catalog>
[OperationBehavior]
public BookResponse GetData(BookRequest request)
{
XmlDocument xml = new XmlDocument();
string filepath = "D:\\BookService\\SampleDB.xml";
xml.Load(filepath);
BookResponse response = new BookResponse();
if (null == request)
response.bookXML = "Error!";
else
response.bookXML = xml.OuterXml;
return response;
}
in my client web project i have
BookRequest request = new BookRequest();
BookServiceClient objBookService = new BookServiceClient();
BookResponse response = objBookService.GetData(request);
testClient.BookService.Book Test2;
Test2 = DeSerializeAnObject(response.bookXML, typeof(testClient.BookService.Book)) as Book;
public static Object DeSerializeAnObject(string XmlOfAnObject, Type ObjectType)
{
StringReader StrReader = new StringReader(XmlOfAnObject);
XmlSerializer Xml_Serializer = new XmlSerializer(ObjectType);
XmlTextReader XmlReader = new XmlTextReader(StrReader);
try
{
Object AnObject = Xml_Serializer.Deserialize(XmlReader);
return AnObject;
}
finally
{
XmlReader.Close();
StrReader.Close();
}
}
Quick glance it seems your contract is saying that the root element is "Book", but I see in the xml being retrieved it has "catalog". It seems as though it is expecting a namespace, have you tried to add that?
I had this problem not too far back and had this bookmarked...
yes by changing the root element from catalog to Book it worked .But after deserialization ,the values in the nodes of the xml is not getting into the book object .please help me where i am doing wrong.
Now the xml looks like:
<?xml version="1.0" encoding="utf-8" ?>
<Book>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>
An in-depth look at creating applications
with XML.
</description>
</book>
<book id="bk102">
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-12-16</publish_date>
<description>
A former architect battles corporate zombies,
an evil sorceress, and her own childhood to become queen
of the world.
</description>
</book>
</Book>
I cannot provide you with a complete solution to the problem without viewing more of the code or obtain a better understanding of your implementation. However,there is an anomaly with between the XML and DataContract around the ID property. In the
XML is appears as an Attribute but described as a Element in the DataContract. To solve this you could replace the XmlElement attribute of the ID property with XmlAttribute.
An easy for you at present is to just use the model class with List<Class> instance to generate an xml file with the help of XmlSerializing class,and then try to see what's the most difference between that and yours……Then you can do changes to the xml file
to apply……
suvo
Member
2 Points
255 Posts
There is an error in XML document (1, 40).Inner exception:- {" was not expected."}Please help me
Jul 08, 2012 06:40 PM|LINK
My wcf service is returning a xml string which i am trying to deserialize to objects from the client.I am getting this type of error
There is an error in XML document (1, 40).
Inner exception:-
{"<catalog xmlns=''> was not expected."}
In my service project I have my Book class like
namespace BookService
{
[DataContract]
//[Serializable, XmlRoot("Book")]
[XmlRoot("Book")]
public class Book
{
[DataMember]
[XmlElement(ElementName = "ID")]
public string ID;
[DataMember]
[XmlElement(ElementName = "Title")]
public string Title;
[DataMember]
[XmlElement(ElementName = "Author")]
public string Author;
[DataMember]
[XmlElement(ElementName = "Description")]
public string Description;
[DataMember]
[XmlElement(ElementName = "Genre")]
public string Genre;
[DataMember]
[XmlElement(ElementName = "Price")]
public decimal Price;
[DataMember]
[XmlElement(ElementName = "PublishDate")]
public DateTime PublishDate;
}
}
The xml that the service is returning is from a xml file which looks like:-
<?xml version="1.0" encoding="utf-8"?>
<catalog >
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>
An in-depth look at creating applications
with XML.
</description>
</book>
<book id="bk102">
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-12-16</publish_date>
<description>
A former architect battles corporate zombies,
an evil sorceress, and her own childhood to become queen
of the world.
</description>
</book>
</catalog>
[OperationBehavior]
public BookResponse GetData(BookRequest request)
{
XmlDocument xml = new XmlDocument();
string filepath = "D:\\BookService\\SampleDB.xml";
xml.Load(filepath);
BookResponse response = new BookResponse();
if (null == request)
response.bookXML = "Error!";
else
response.bookXML = xml.OuterXml;
return response;
}
in my client web project i have
BookRequest request = new BookRequest();
BookServiceClient objBookService = new BookServiceClient();
BookResponse response = objBookService.GetData(request);
testClient.BookService.Book Test2;
Test2 = DeSerializeAnObject(response.bookXML, typeof(testClient.BookService.Book)) as Book;
public static Object DeSerializeAnObject(string XmlOfAnObject, Type ObjectType)
{
StringReader StrReader = new StringReader(XmlOfAnObject);
XmlSerializer Xml_Serializer = new XmlSerializer(ObjectType);
XmlTextReader XmlReader = new XmlTextReader(StrReader);
try
{
Object AnObject = Xml_Serializer.Deserialize(XmlReader);
return AnObject;
}
finally
{
XmlReader.Close();
StrReader.Close();
}
}
Please help me to solve the problem
</div>cornball76
Participant
1126 Points
210 Posts
Re: There is an error in XML document (1, 40).Inner exception:- {" was not expected."}Please help...
Jul 09, 2012 03:49 AM|LINK
Quick glance it seems your contract is saying that the root element is "Book", but I see in the xml being retrieved it has "catalog". It seems as though it is expecting a namespace, have you tried to add that?
I had this problem not too far back and had this bookmarked...
http://stackoverflow.com/questions/1556874/user-xmlns-was-not-expected-deserializing-twitter-xml
suvo
Member
2 Points
255 Posts
Re: There is an error in XML document (1, 40).Inner exception:- {" was not expected."}Please help...
Jul 09, 2012 08:30 AM|LINK
yes by changing the root element from catalog to Book it worked .But after deserialization ,the values in the nodes of the xml is not getting into the book object .please help me where i am doing wrong.
Now the xml looks like:
<?xml version="1.0" encoding="utf-8" ?>
<Book>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>
An in-depth look at creating applications
with XML.
</description>
</book>
<book id="bk102">
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-12-16</publish_date>
<description>
A former architect battles corporate zombies,
an evil sorceress, and her own childhood to become queen
of the world.
</description>
</book>
</Book>
cornball76
Participant
1126 Points
210 Posts
Re: There is an error in XML document (1, 40).Inner exception:- {" was not expected."}Please help...
Jul 09, 2012 06:23 PM|LINK
Take a look at your contract. Right now you have it setup to take in 1 book... but your xml has several books.
suvo
Member
2 Points
255 Posts
Re: There is an error in XML document (1, 40).Inner exception:- {" was not expected."}Please help...
Jul 09, 2012 07:27 PM|LINK
Actually i wanted all the nodevalues to a generic list .
I could achieve..
List<Book> lstBook = new List<Book>();
Book objBook = null;
XmlDocument xdoc = new XmlDocument();
xdoc.LoadXml(response.bookXML);
XmlNodeList list = xdoc.GetElementsByTagName("book");
for (int i = 0; i < list.Count; i++)
{
objBook = new Book();
XmlElement author = (XmlElement)xdoc.GetElementsByTagName("author")[i];
objBook.Author = author.InnerText;
lstBook.Add(objBook);
}
alec2012
Member
40 Points
6 Posts
Re: There is an error in XML document (1, 40).Inner exception:- {" was not expected."}Please help...
Jul 09, 2012 08:46 PM|LINK
I cannot provide you with a complete solution to the problem without viewing more of the code or obtain a better understanding of your implementation. However, there is an anomaly with between the XML and DataContract around the ID property. In the XML is appears as an Attribute but described as a Element in the DataContract. To solve this you could replace the XmlElement attribute of the ID property with XmlAttribute.
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: There is an error in XML document (1, 40).Inner exception:- {" was not expected."}Please help...
Jul 10, 2012 01:39 AM|LINK
Hello:)
An easy for you at present is to just use the model class with List<Class> instance to generate an xml file with the help of XmlSerializing class,and then try to see what's the most difference between that and yours……Then you can do changes to the xml file to apply……
Reguards!