Tuesday, May 3, 2011

XmlInclude or SoapInclude

I am developing a webservice that returns arrays of classes I define within the webservice. When I test it, I get: "System.InvalidOperationException: The type WebSite+HostHeader was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically."

Here is part of the code:

[WebService(Namespace = "http://WebSiteInfo.Podiumcrm.com/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] public class WebSite : System.Web.Services.WebService { public class WebSiteEntry { public string SiteName = ""; public string Comment = ""; public string IISPath = ""; public int SiteID = 0; public ArrayList HostHeaders;

    public WebSiteEntry()
    {
    }
}
public class HostHeader
{
    public string IPAddress = "";
    public int Port = 0;
    public string URL = "";

    public HostHeader()
    {
    }
}


[WebMethod(EnableSession = true)]
[TraceExtension(Filename = @"C:\DotNetLogs\WebSiteServices.log")]
public WebSiteEntry[] WebSites()
{...}

}

When I try: [WebService(Namespace = "http://WebSiteInfo.Podiumcrm.com/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [XmlInclude(typeof(WebSiteEntry))] [XmlInclude(typeof(WebSiteProperty))] [XmlInclude(typeof(HostHeader))]

public class WebSite : System.Web.Services.WebService {...}

I get: The type or namespace name 'XmlInclude' could not be found (are you missing a using directive or an assembly reference?)

Points the the person who can give me the incantation that both compiles and executes!

Thanks...

From stackoverflow
  • For shits and giggles, add the [Serializable] attribute to your HostHeader class:

    [Serializable]
    public class HostHeader
    {    
        private string _ipAddress = "";
        private int _port = 0;
        private string _url = "";
    
        public string IpAddress { get { return _ipAddress; } set { _ipAddress = value; } }
    
        public int Port { get { return _port; } set { _port = value; } }
    
        public string Url { get { return _url; } set { _url = value; } }
    
        public HostHeader()
        {
        }
    }
    

    That should ensure that your class is XML Serializable.

    alexdej : Actually, the XmlSerializer ignores this attribute.
  • From the error you are receiving:

    The type or namespace name 'XmlInclude' could not be found (are you missing a using directive or an assembly reference?)

    It appears that you are missing the System.Xml.Serialization Namespace. You can fully qualifying the XmlInclude type, like this:

    System.Xml.Serialization.XmlInclude(typeof(WebSiteProperty))
    

    or add the namespace via the using directive:

    using System.Xml.Serialization
    

0 comments:

Post a Comment