Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Please help! XML Deserialization to Class issue in C#

  • 17-09-2010 02:33PM
    #1
    Registered Users, Registered Users 2 Posts: 250 ✭✭


    This is the XML I am trying to parse:
    <?xml version="1.0" ?> 
    <result>
    	<context>W3Svc</context> 
    	<command>EnumSites</command> 
    	<status>success</status> 
    	<sitecount>3</sitecount> 
    	<sites>
    		<site>
    			<id>1</id> 
    			<state>Stopped</state> 
    			<comment>Default Web Site</comment> 
    		</site>
    		<site>
    			<id>2</id> 
    			<state>Started</state> 
    			<comment>iisman</comment> 
    		</site>
    		<site>
    			<id>3</id> 
    			<state>Stopped</state> 
    			<comment>test2</comment> 
    		</site>
    	</sites>
    </result>
    

    and this is my class:
    public class Site
        {
            private int __id = 0;
            [XmlElement(ElementName = "id")]
            public int Id { get { return __id; } set { __id = value; } }
        }
    
        [XmlRoot(ElementName = "result")]
        public class EnumSitesClass : ResponseHeader
        {
            private int __siteCount = 0;
            [XmlElement(ElementName = "sitecount")]
            public int SiteCount { get { return __siteCount; } set { __siteCount = value; } }
    
            [XmlArray("Sites")]
            [XmlArrayItem("Site", typeof(Site))]
            public Site[] SiteArray;
        }
    
        public class EnumSites
        {
            private EnumSitesClass m_response = null;
    
            public EnumSitesClass Get { get { return m_response; } }
    
            public EnumSites(string hostName, int timeOut)
            {
                EnumSitesClass test = new EnumSitesClass();
                HTTP http = new HTTP(timeOut);
    
                Interface adsi = new Interface(hostName);
    
                string interfaceUrl = adsi.GetInterfaceUrl(InterfaceCommandUrl.EnumSites, Context.W3Svc, HttpConnType.http);
                string ret = http.GetHttp(interfaceUrl);
    
                StringReader reader = new StringReader(ret);
                XmlSerializer s = new XmlSerializer(typeof(EnumSitesClass));
                m_response = (EnumSitesClass)s.Deserialize(reader);
            }
        }
    }
    
    EnumSites es = new EnumSites("127.0.0.1", 6000);
                EnumSitesClass esc = es.Get;
    
    esc.SiteArray remains null for some bizarre reason after execution. Am I going mad???

    Any help would be appreciated.


Comments

  • Registered Users, Registered Users 2 Posts: 12,025 ✭✭✭✭Giblet


    You're going to kick yourself.
    [XmlArray("sites")]
    [XmlArrayItem("site", typeof(Site))]
    

    Case sensitive...

    Happens to us all :)


  • Registered Users, Registered Users 2 Posts: 250 ✭✭ikoonman


    Now I feel like an idiot - LOL - thanks for your help.


Advertisement