This is my first time using XML serialization and it makes me absolutely nuts after 2 days trying to fix this problem.
I get this error when deserialization starts:
The XML element 'name' from namespace '' is already present in the current scope. Use XML attributes to specify another XML name or namespace for the element.
The error in this line in my code is:
Album album = (Album)serializer.Deserialize(reader);
I'm not sure why. There is no duplicate "name" node, so I just don't get it. This is an XML document obtained from HttpWebResponse from a third-party REST API.
Here is the full code:
My album class (the type by which I deserialize):
public class Album { #region Constructors public Album() { } #endregion #region ElementConstants public static class ElementConstants { public const string aID = "aid"; public const string Owner = "owner"; public const string AlbumName = "name"; public const string CoverPhotoID = "cover_pid"; public const string CreateDate = "created"; public const string LastModifiedDate = "modified"; public const string Description = "description"; public const string Location = "location"; public const string AlbumURL = "link"; public const string Size = "size"; public const string Visible = "visible"; } #endregion ElementConstants #region Public Properties [XmlArray(ElementName = "photos_GetAlbums_response")] [XmlArrayItem( "album" )] public Album[] Albums { get; set; } [XmlElement (ElementName = ElementConstants.AlbumName, DataType = "string")] public string AlbumID { get; set; } [XmlElement(ElementName = ElementConstants.aID, DataType = "int")] public Int32 CoverPhotoID { get; set; } [XmlElement(ElementName = ElementConstants.Owner, DataType = "string")] public string Owner { get; set; } [XmlElement(ElementName = ElementConstants.AlbumName, DataType = "string")] public string AlbumName { get; set; } [XmlElement(ElementName = ElementConstants.aID, DataType = "DateTime")] public DateTime CreateDate { get; set; } [XmlElement(ElementName = ElementConstants.LastModifiedDate, DataType = "DateTime")] public DateTime LastModifiedDate { get; set; } [XmlElement(ElementName = ElementConstants.Description, DataType = "string")] public string Description { get; set; } [XmlElement(ElementName = ElementConstants.Location, DataType = "string")] public string Location { get; set; } [XmlElement(ElementName = ElementConstants.AlbumURL, DataType = "string")] public string Link { get; set; } [XmlElement(ElementName = ElementConstants.Size, DataType = "size")] public string Size { get; set; } [XmlElement(ElementName = ElementConstants.Visible, DataType = "string")] public string Visible { get; set; } #endregion }
My Serializer class :
public class Serializer { public static Album CreateAlbumFromXMLDoc(XmlDocument doc) {
The XML I'm trying to destroy (copied from an Xml Doc object that is passed to the CreateAlbumFromXMLDoc method when debugging in VS):
<?xml version="1.0" encoding="UTF-8"?> <photos_GetAlbums_response xsi:schemaLocation="http://api.example.com/1.0/ http://api.example.com/1.0/xxx.xsd" list="true"> <album> <aid>3231990241086938677</aid> <cover_pid>7031990241087042549</cover_pid> <owner>1337262814</owner> <name>LA</name> <created>1233469624</created> <modified>1233469942</modified> <description>trip to LA</description> <location>CA</location> <link>http://www.example.com/album.php?aid=7333&id=1337262814</link> <size>48</size> <visible>friends</visible> </album> <album> <aid>7031990241086936240</aid> <cover_pid>7031990241087005994</cover_pid> <owner>1337262814</owner> <name>Wall Photos</name> <created>1230437805</created> <modified>1233460690</modified> <description/> <location/> <link>http://www.example.com/album.php?aid=3296&id=1337262814</link> <size>34</size> <visible>everyone</visible> </album> <album> <aid>7031990241086937544</aid> <cover_pid>7031990241087026027</cover_pid> <owner>1337262814</owner> <name>Mobile Uploads</name> <created>1231984989</created> <modified>1233460349</modified> <description/> <location/> <link>http://www.example.com/album.php?aid=6300&id=1337262814</link> <size>3</size> <visible>friends</visible> </album> <album> <aid>7031990241086936188</aid> <cover_pid>7031990241087005114</cover_pid> <owner>1337262814</owner> <name>Christmas 2008</name> <created>1230361978</created> <modified>1230362306</modified> <description>My Album</description> <location/> <link>http://www.example.com/album.php?aid=5234&id=1337262814</link> <size>50</size> <visible>friends</visible> </album> <album> <aid>7031990241086935881</aid> <cover_pid>7031990241087001093</cover_pid> <owner>1637262814</owner> <name>Hock</name> <created>1229889219</created> <modified>1229889235</modified> <description>Misc Pics</description> <location/> <link>http://www.example.com/album.php?aid=4937&id=1637262814</link> <size>1</size> <visible>friends-of-friends</visible> </album> <album> <aid>7031990241086935541</aid> <cover_pid>7031990241086996817</cover_pid> <owner>1637262814</owner> <name>Test Album 2 (for work)</name> <created>1229460455</created> <modified>1229460475</modified> <description>this is a test album</description> <location/> <link>http://www.example.com/album.php?aid=4547&id=1637262814</link> <size>1</size> <visible>everyone</visible> </album> <album> <aid>7031990241086935537</aid> <cover_pid>7031990241086996795</cover_pid> <owner>1637262814</owner> <name>Test Album (for work)</name> <created>1229459168</created> <modified>1229459185</modified> <description>Testing for work</description> <location/> <link>http://www.example.com/album.php?aid=4493&id=1637262814</link> <size>1</size> <visible>friends</visible> </album> </photos_GetAlbums_response>
Note: Just hell, I paste this XML into an XML 2007 notepad, it tells me:
The XML document does not contain xml-stylesheet processing instructions. To enable XSLT conversion, add the following to the top of the file and edit the href attribute accordingly:
I do not think that this really means that it is distorted or something like that, but just a note.
So..
My ultimate goal is to get this damn mistake and get an array of albums using my code above as soon as I can get past the error. I also want to make sure my code is correct when trying to restore this album using the Album [] property in my Album class or something else that I might not see here. I think this is pretty close and should work, but it is not.
Follow-up Since then I have been pulling my hair out.
Here is the last one. At the moment I have not used some things (from Mark) like Enum, etc. I could change this later. I also pulled out datetime stuff as it just looked weird and I didn't get any errors on this for now ... at least for now. The main problem now is my damn XML.
Is he still having problems with the format I assume? If this does not cover another problem, he has no idea. It drives me crazy.
Now I get this error when deserialization kicks in :
Data at the root level is invalid. Line 1, position 1.
The error in this line is in my code: GetAlbumsResponse album = (GetAlbumsResponse) serializer.Deserialize (reader);
How to get the answer in an XmL document :
public static XmlDocument GetResponseXmlDocument(HttpWebResponse response) { Stream dataStream = null;
My album class and root level class (thanks to help from Marc..I get it now):
namespace xxx.Entities { [Serializable, XmlRoot("photos_GetAlbums_response")] public class GetAlbumsResponse { [XmlElement("album")] public List<Album> Albums { get; set; } [XmlAttribute("list")] public bool IsList { get; set; } } public class Album { #region Constructors public Album() { } #endregion #region ElementConstants /// <summary> /// Constants Class to eliminate use of Magic Strings (hard coded strings) /// </summary> public static class ElementConstants { public const string aID = "aid"; public const string Owner = "owner"; public const string AlbumName = "name"; public const string CoverPhotoID = "cover_pid"; public const string CreateDate = "created"; public const string LastModifiedDate = "modified"; public const string Description = "description"; public const string Location = "location"; public const string AlbumURL = "link"; public const string Size = "size"; public const string Visible = "visible"; } #endregion ElementConstants #region Public Properties [XmlElement (ElementName = ElementConstants.aID, DataType = "string")] public string AlbumID { get; set; } [XmlElement(ElementName = ElementConstants.CoverPhotoID, DataType = "int")] public Int32 CoverPhotoID { get; set; } [XmlElement(ElementName = ElementConstants.Owner, DataType = "string")] public string Owner { get; set; } [XmlElement(ElementName = ElementConstants.AlbumName, DataType = "string")] public string AlbumName { get; set; } public string Created { get; set; } public DateTime Modified { get; set; } [XmlElement(ElementName = ElementConstants.Description, DataType = "string")] public string Description { get; set; } [XmlElement(ElementName = ElementConstants.Location, DataType = "string")] public string Location { get; set; } [XmlElement(ElementName = ElementConstants.AlbumURL, DataType = "string")] public string Link { get; set; } public string Size { get; set; } [XmlElement(ElementName = ElementConstants.Visible, DataType = "string")] public string Visible { get; set; } #endregion } }
My Serializer class:
namespace xxx.Utilities { public class Serializer { public static List<Album> CreateAlbumFromXMLDoc(XmlDocument doc) {
True xml input which I am trying to Deserialize (yes, it has xmlns):
<?xml version="1.0" encoding="UTF-8"?> <photos_GetAlbums_response xmlns="http://api.example.com/1.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://api.example.com/1.0/ http://api.example.com/1.0/xxx.xsd" list="true"> <album> <aid>7321990241086938677</aid> <cover_pid>7031990241087042549</cover_pid> <owner>1124262814</owner> <name>Album Test 1</name> <created>1233469624</created> <modified>1233469942</modified> <description>Our trip</description> <location>CA</location> <link>http://www.example.com/album.php?aid=7733&id=1124262814</link> <size>48</size> <visible>friends</visible> </album> <album> <aid>231990241086936240</aid> <cover_pid>7042330241087005994</cover_pid> <owner>1124262814</owner> <name>Album Test 2</name> <created>1230437805</created> <modified>1233460690</modified> <description /> <location /> <link>http://www.example.com/album.php?aid=5296&id=1124262814</link> <size>34</size> <visible>everyone</visible> </album> <album> <aid>70319423341086937544</aid> <cover_pid>7032390241087026027</cover_pid> <owner>1124262814</owner> <name>Album Test 3</name> <created>1231984989</created> <modified>1233460349</modified> <description /> <location /> <link>http://www.example.com/album.php?aid=6600&id=1124262814</link> <size>3</size> <visible>friends</visible> </album> </photos_GetAlbums_response>