Deserialization Error: The XML element 'name' from namespace '' is already present in the current scope

前端 未结 9 1188
旧巷少年郎
旧巷少年郎 2020-12-18 07:43

This is my first time using XML Serialization and this is driving me absolutely nuts after 2 days of trying to troubleshoot this.

I get this error w

相关标签:
9条回答
  • 2020-12-18 08:06

    Ok - I coded up an example. I took a look at the Facebook API, now here is a FULL working example. Try this:

    [XmlRoot("photos_getAlbums_response", Namespace="http://api.facebook.com/1.0/")]
    public class GetAlbumsResponse
    {
        public GetAlbumsResponse() 
        {    
        }
    
        [XmlElement("album")]
        public List<Album> Albums { get; set; }
    }
    
    public class Album
    {
        [XmlElement("aid")]
        public long Aid{get;set;}
    
        [XmlElement("cover_pid")]
        public long CoverPid{get;set;}
    
        [XmlElement("owner")]
        public long Owner{get;set;}
    
        [XmlElement("name")]
        public string Name{get;set;}
    
        [XmlElement("created")]
        public long Created{get;set;}
    
        [XmlElement("modified")]
        public long Modified{get;set;}
    
        [XmlElement("description")]
        public string Description{get;set;}
    
        [XmlElement("location")]
        public string Location{get;set;}
    
        [XmlElement("link")]
        public string Link{get;set;}
    
        [XmlElement("size")]
        public int Size{get;set;}
    
        [XmlElement("visible")]
        public string Visible{get;set;}
    
        public Album()
        {}
    }
    
    class XmlUtils
    {
        public static T DeserializeFromXml<T>(string xml)
        {
            T result;
            XmlSerializer ser = new XmlSerializer(typeof(T));
            using (TextReader tr = new StringReader(xml))
            {
                result = (T)ser.Deserialize(tr);
            }
            return result;
        }
    }
    

    Now.. with an xml photos_getAlbums_response from the Facebook API,

    You can deserialize like this:

     GetAlbumsResponse response = XmlUtils.DeserializeFromXml<GetAlbumsResponse>(xmlResponseString);
    
    0 讨论(0)
  • 2020-12-18 08:12

    Use System.Xml.XmlDocument to parse the input. It shouldn't take more than an hour to write the code to extract the data yourself.

    0 讨论(0)
  • 2020-12-18 08:15

    Personally, I wouldn't use constants here - they make it hard to spot errors (and since you probably aren't re-using them, don't add much). For example:

        [XmlElement (ElementName = ElementConstants.AlbumName, DataType = "string")]
        public string AlbumID { get; set; }
    ...
        [XmlElement(ElementName = ElementConstants.AlbumName, DataType = "string")]
        public string AlbumName { get; set; }
    

    Looks suspect to me...

    An easier approach is to write the xml you want to a file (foo.xml, say) and use:

    xsd foo.xml
    xsd foo.xsd /classes
    

    Then look at foo.cs.

    0 讨论(0)
  • 2020-12-18 08:15

    Here we go... note the xml was invalid (& should be &amp;; use of undeclared xsi namespace-alias). Note also that I added an enum for the visibility, added handling for converting the long to DateTime, and added the wrapper type:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.IO;
    using System.Xml;
    using System.Xml.Serialization;
    
    static class Program
    {
        const string xml = @"<?xml version=""1.0"" encoding=""UTF-8""?>
    <photos_GetAlbums_response
        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>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&amp;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&amp;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&amp;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&amp;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&amp;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&amp;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&amp;id=1637262814</link>
     <size>1</size>
     <visible>friends</visible>
     </album>
     </photos_GetAlbums_response>";
        static void Main()
        {
            XmlSerializer ser = new XmlSerializer(typeof(GetAlbumsResponse));
            GetAlbumsResponse response;
            using (StringReader reader = new StringReader(xml))
            {
                response = (GetAlbumsResponse)ser.Deserialize(reader);
            }
    
        }
    }
    
    [Serializable, XmlRoot("photos_GetAlbums_response")]
    public class GetAlbumsResponse
    {
        [XmlElement("album")]
        public List<Album> Albums {get;set;}
    
        [XmlAttribute("list")]
        public bool IsList { get; set; }
    }
    public enum AlbumVisibility
    {
        [XmlEnum("")]
        None,
        [XmlEnum("friends")]
        Friends,
        [XmlEnum("friends-of-friends")]
        FriendsOfFriends,
        [XmlEnum("everyone")]
        Everyone
    
    }
    [Serializable]
    public class Album
    {
        static readonly DateTime epoch = new DateTime(1970, 1, 1);
        static long SerializeDateTime(DateTime value)
        {
            return (long)((value - epoch).TotalSeconds);
        }
        static DateTime DeserializeDateTime(long value)
        {
            return epoch.AddSeconds(value);
        }
        [XmlElement("aid")]
        public long AlbumID { get; set; }
    
        [XmlElement("cover_pid")]
        public long CoverPhotoID { get; set; }
    
        [XmlElement("owner")]
        public long Owner { get; set; }
    
        [XmlElement("name")]
        public string AlbumName { get; set; }
    
        [XmlIgnore]
        public DateTime CreateDate { get; set; }
    
        [XmlElement("created"), Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public long CreateDateInt64 {
            get {return SerializeDateTime(CreateDate);}
            set {CreateDate = DeserializeDateTime(value);}
        }
    
        [XmlIgnore]
        public DateTime LastModifiedDate { get; set; }
    
        [XmlElement("modified"), Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public long LastModifiedDateInt64
        {
            get { return SerializeDateTime(LastModifiedDate); }
            set { LastModifiedDate = DeserializeDateTime(value); }
        }
    
        [XmlElement("description")]
        public string Description { get; set; }
    
        [XmlElement("location")]
        public string Location { get; set; }
    
        [XmlElement("link")]
        public string Link { get; set; }
    
        [XmlElement("size")]
        public int Size { get; set; }
    
        [XmlElement("visible")]
        public AlbumVisibility Visibility { get; set; }
    }
    
    0 讨论(0)
  • 2020-12-18 08:19

    Well,

    there is a link from Microsoft targeting your problem

    0 讨论(0)
  • 2020-12-18 08:20

    The Xml that would work for your current code is something like this:

    <Album><photos_GetAlbums_response>
    <Album>
       <photos_GetAlbums_response>
          <Album>
             <photos_GetAlbums_response> ....
    

    A response, which has an array of Albums, where each Album has a response which is an Array of Albums...etc.

    Anyway, I already helped you in your other question, and even went to the trouble of creating a full working code sample. Why did you create another question for the same problem ?

    0 讨论(0)
提交回复
热议问题