title: Deserialization Error: The XML element 'name' from namespace '' is already present in the current scope
body: <p>This is my first time using XML Serialization and this is driving me absolutely <strong>nuts</strong> after 2 days of trying to troubleshoot this.</p>
  
  <p>I get this error when the deserialization kicks in:</p>
  
  <pre><code>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.
  </code></pre>
  
  <p>The error happens on this line in my code:</p>
  
  <pre><code>Album album = (Album)serializer.Deserialize(reader);
  </code></pre>
  
  <p>I not sure why.  There is no dup "name" node so I just don't get it.  This is an XML doc received from an HttpWebResponse from a 3rd party REST API.</p>
  
  <p><strong>Here's the complete code:</strong></p>
  
  <p><strong>My Album Class</strong> (the type I'm Deserializing to):</p>
  
  <pre><code>    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
      }
  </code></pre>
  
  <p><strong>My Serializer Class</strong>:</p>
  
  <pre><code>    public class Serializer
      {
          public static Album CreateAlbumFromXMLDoc(XmlDocument doc)
          {
              // Create an instance of a serializer
              var serializer = new XmlSerializer(typeof(Album));
              var reader = new StringReader(doc.ToString());
  
              // Deserialize the Xml Object and cast to type Album
              Album album = (Album)serializer.Deserialize(reader);
  
              return album;
          }
      }
  </code></pre>
  
  <p><strong>The XML that I am trying to Deserialized</strong> (copied from the Xml Doc object being passed into the CreateAlbumFromXMLDoc method when debugging in VS):</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;photos_GetAlbums_response xsi:schemaLocation="http://api.example.com/1.0/ http://api.example.com/1.0/xxx.xsd" list="true"&gt;
  &lt;album&gt;
   &lt;aid&gt;3231990241086938677&lt;/aid&gt;
   &lt;cover_pid&gt;7031990241087042549&lt;/cover_pid&gt;
   &lt;owner&gt;1337262814&lt;/owner&gt;
   &lt;name&gt;LA&lt;/name&gt;
   &lt;created&gt;1233469624&lt;/created&gt;
   &lt;modified&gt;1233469942&lt;/modified&gt;
   &lt;description&gt;trip to LA&lt;/description&gt;
   &lt;location&gt;CA&lt;/location&gt;
   &lt;link&gt;http://www.example.com/album.php?aid=7333&amp;id=1337262814&lt;/link&gt;
   &lt;size&gt;48&lt;/size&gt;
   &lt;visible&gt;friends&lt;/visible&gt;
   &lt;/album&gt;
  &lt;album&gt;
   &lt;aid&gt;7031990241086936240&lt;/aid&gt;
   &lt;cover_pid&gt;7031990241087005994&lt;/cover_pid&gt;
   &lt;owner&gt;1337262814&lt;/owner&gt;
   &lt;name&gt;Wall Photos&lt;/name&gt;
   &lt;created&gt;1230437805&lt;/created&gt;
   &lt;modified&gt;1233460690&lt;/modified&gt;
   &lt;description/&gt;
   &lt;location/&gt;
   &lt;link&gt;http://www.example.com/album.php?aid=3296&amp;id=1337262814&lt;/link&gt;
   &lt;size&gt;34&lt;/size&gt;
   &lt;visible&gt;everyone&lt;/visible&gt;
   &lt;/album&gt;
  &lt;album&gt;
   &lt;aid&gt;7031990241086937544&lt;/aid&gt;
   &lt;cover_pid&gt;7031990241087026027&lt;/cover_pid&gt;
   &lt;owner&gt;1337262814&lt;/owner&gt;
   &lt;name&gt;Mobile Uploads&lt;/name&gt;
   &lt;created&gt;1231984989&lt;/created&gt;
   &lt;modified&gt;1233460349&lt;/modified&gt;
   &lt;description/&gt;
   &lt;location/&gt;
   &lt;link&gt;http://www.example.com/album.php?aid=6300&amp;id=1337262814&lt;/link&gt;
   &lt;size&gt;3&lt;/size&gt;
   &lt;visible&gt;friends&lt;/visible&gt;
   &lt;/album&gt;
  &lt;album&gt;
   &lt;aid&gt;7031990241086936188&lt;/aid&gt;
   &lt;cover_pid&gt;7031990241087005114&lt;/cover_pid&gt;
   &lt;owner&gt;1337262814&lt;/owner&gt;
   &lt;name&gt;Christmas 2008&lt;/name&gt;
   &lt;created&gt;1230361978&lt;/created&gt;
   &lt;modified&gt;1230362306&lt;/modified&gt;
   &lt;description&gt;My Album&lt;/description&gt;
   &lt;location/&gt;
   &lt;link&gt;http://www.example.com/album.php?aid=5234&amp;id=1337262814&lt;/link&gt;
   &lt;size&gt;50&lt;/size&gt;
   &lt;visible&gt;friends&lt;/visible&gt;
   &lt;/album&gt;
  &lt;album&gt;
   &lt;aid&gt;7031990241086935881&lt;/aid&gt;
   &lt;cover_pid&gt;7031990241087001093&lt;/cover_pid&gt;
   &lt;owner&gt;1637262814&lt;/owner&gt;
   &lt;name&gt;Hock&lt;/name&gt;
   &lt;created&gt;1229889219&lt;/created&gt;
   &lt;modified&gt;1229889235&lt;/modified&gt;
   &lt;description&gt;Misc Pics&lt;/description&gt;
   &lt;location/&gt;
   &lt;link&gt;http://www.example.com/album.php?aid=4937&amp;id=1637262814&lt;/link&gt;
   &lt;size&gt;1&lt;/size&gt;
   &lt;visible&gt;friends-of-friends&lt;/visible&gt;
   &lt;/album&gt;
  &lt;album&gt;
   &lt;aid&gt;7031990241086935541&lt;/aid&gt;
   &lt;cover_pid&gt;7031990241086996817&lt;/cover_pid&gt;
   &lt;owner&gt;1637262814&lt;/owner&gt;
   &lt;name&gt;Test Album 2 (for work)&lt;/name&gt;
   &lt;created&gt;1229460455&lt;/created&gt;
   &lt;modified&gt;1229460475&lt;/modified&gt;
   &lt;description&gt;this is a test album&lt;/description&gt;
   &lt;location/&gt;
   &lt;link&gt;http://www.example.com/album.php?aid=4547&amp;id=1637262814&lt;/link&gt;
   &lt;size&gt;1&lt;/size&gt;
   &lt;visible&gt;everyone&lt;/visible&gt;
   &lt;/album&gt;
  &lt;album&gt;
   &lt;aid&gt;7031990241086935537&lt;/aid&gt;
   &lt;cover_pid&gt;7031990241086996795&lt;/cover_pid&gt;
   &lt;owner&gt;1637262814&lt;/owner&gt;
   &lt;name&gt;Test Album (for work)&lt;/name&gt;
   &lt;created&gt;1229459168&lt;/created&gt;
   &lt;modified&gt;1229459185&lt;/modified&gt;
   &lt;description&gt;Testing for work&lt;/description&gt;
   &lt;location/&gt;
   &lt;link&gt;http://www.example.com/album.php?aid=4493&amp;id=1637262814&lt;/link&gt;
   &lt;size&gt;1&lt;/size&gt;
   &lt;visible&gt;friends&lt;/visible&gt;
   &lt;/album&gt;
   &lt;/photos_GetAlbums_response&gt;
  </code></pre>
  
  <p><strong>A side note:</strong> Just for the hell of it, I paste that XML into XML Notepad 2007, it tells me:</p>
  
  <p>Your XML document contains no xml-stylesheet processing instruction. To provide an XSLT transform, add the following to the top of your file and edit the href attribute accordingly:</p>
  
  
  
  <p>I don't think that really means it's malformed or anything but just something to note.</p>
  
  <p><strong>So..</strong></p>
  
  <p>My ultimate goal is to get pass this damn error obviously and get an array of albums back using my code above once I can get past the error.  I also want to make sure my code is correct in trying to retrieve that arrray back of albums using my Album[] property in my Album class or anything else I might be missing here.  I think it's pretty close and should work but it's not. </p>
  
  <hr>
  
  <p>Follow-up.  I've been pulling my hair out since then.</p>
  
  <p>Here's the latest.  I did not use some things for now (from Marc) like the Enum, etc.  I might change that later.  I also pulled out the datetime stuff as it just looked wierd and I did not get errors on that anway without...at least yet.  The main problem now is still my damn XML.</p>
  
  <p>It's still appearing to have problems with the format I guess?  Unless it's covering up another problem, no clue.  This is driving me fing crazy.</p>
  
  <h2><strong>I now get this error when the deserialization kicks in</strong>:</h2>
  
  <pre><code>Data at the root level is invalid. Line 1, position 1.
  </code></pre>
  
  <p>The error happens on this line in my code: GetAlbumsResponse album = (GetAlbumsResponse)serializer.Deserialize(reader);</p>
  
  <p><strong>How I get the response into an XmL doc</strong>:</p>
  
  <pre><code>public static XmlDocument GetResponseXmlDocument(HttpWebResponse response)
          {
              Stream dataStream = null; // stream from WebResponse
              XmlDocument doc = new XmlDocument();
  
              if (doc == null)
              {
                  throw new NullReferenceException("The web reponse was null");
              }
  
              // Get the response stream so we can read the body of the response
              dataStream = response.GetResponseStream();
  
              // Open the stream using a StreamReader for easy access
              StreamReader reader = new StreamReader(dataStream);
  
              // Load response into string variable so that we can then load into an XML doc
              string responseString = reader.ReadToEnd();
  
              // Create an XML document &amp; load it with the response data
              doc.LoadXml(responseString);
  
              // Final XML document that represents the response
              return doc;
          }
  </code></pre>
  
  <h2><strong>My Album Class &amp; Root Level Class</strong> (thanks to help from Marc..I get it now):</h2>
  
  <pre><code>namespace xxx.Entities
  {
  
      [Serializable, XmlRoot("photos_GetAlbums_response")]
      public class GetAlbumsResponse
      {
          [XmlElement("album")]
          public List&lt;Album&gt; Albums { get; set; }
  
          [XmlAttribute("list")]
          public bool IsList { get; set; }
      }
  
      public class Album
      {
          #region Constructors
  
          public Album()
          {
  
          }
  
          #endregion
  
          #region ElementConstants
  
          /// &lt;summary&gt;
          /// Constants Class to eliminate use of Magic Strings (hard coded strings)
          /// &lt;/summary&gt;
          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
      }
  }
  </code></pre>
  
  <h2>My Serializer Class:</h2>
  
  <pre><code>namespace xxx.Utilities
  {
      public class Serializer
      {
          public static List&lt;Album&gt; CreateAlbumFromXMLDoc(XmlDocument doc)
          {
              // Create an instance of a serializer
              var serializer = new XmlSerializer(typeof(Album));
              var reader = new StringReader(doc.ToString());
  
              // Deserialize the Xml Object and cast to type Album
              GetAlbumsResponse album = (GetAlbumsResponse)serializer.Deserialize(reader);
  
              return album.Albums;
          }
      }
  }
  </code></pre>
  
  <h2>The true XML incoming, that I am trying to Deserialize (yes it does have xmlns):</h2>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;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"&gt;
    &lt;album&gt;
      &lt;aid&gt;7321990241086938677&lt;/aid&gt;
      &lt;cover_pid&gt;7031990241087042549&lt;/cover_pid&gt;
      &lt;owner&gt;1124262814&lt;/owner&gt;
      &lt;name&gt;Album Test 1&lt;/name&gt;
      &lt;created&gt;1233469624&lt;/created&gt;
      &lt;modified&gt;1233469942&lt;/modified&gt;
      &lt;description&gt;Our trip&lt;/description&gt;
      &lt;location&gt;CA&lt;/location&gt;
      &lt;link&gt;http://www.example.com/album.php?aid=7733&amp;id=1124262814&lt;/link&gt;
      &lt;size&gt;48&lt;/size&gt;
      &lt;visible&gt;friends&lt;/visible&gt;
    &lt;/album&gt;
    &lt;album&gt;
      &lt;aid&gt;231990241086936240&lt;/aid&gt;
      &lt;cover_pid&gt;7042330241087005994&lt;/cover_pid&gt;
      &lt;owner&gt;1124262814&lt;/owner&gt;
      &lt;name&gt;Album Test 2&lt;/name&gt;
      &lt;created&gt;1230437805&lt;/created&gt;
      &lt;modified&gt;1233460690&lt;/modified&gt;
      &lt;description /&gt;
      &lt;location /&gt;
      &lt;link&gt;http://www.example.com/album.php?aid=5296&amp;id=1124262814&lt;/link&gt;
      &lt;size&gt;34&lt;/size&gt;
      &lt;visible&gt;everyone&lt;/visible&gt;
    &lt;/album&gt;
    &lt;album&gt;
      &lt;aid&gt;70319423341086937544&lt;/aid&gt;
      &lt;cover_pid&gt;7032390241087026027&lt;/cover_pid&gt;
      &lt;owner&gt;1124262814&lt;/owner&gt;
      &lt;name&gt;Album Test 3&lt;/name&gt;
      &lt;created&gt;1231984989&lt;/created&gt;
      &lt;modified&gt;1233460349&lt;/modified&gt;
      &lt;description /&gt;
      &lt;location /&gt;
      &lt;link&gt;http://www.example.com/album.php?aid=6600&amp;id=1124262814&lt;/link&gt;
      &lt;size&gt;3&lt;/size&gt;
      &lt;visible&gt;friends&lt;/visible&gt;
    &lt;/album&gt;
  &lt;/photos_GetAlbums_response&gt;
  </code></pre>