Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6799115
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:48:36+00:00 2026-05-26T18:48:36+00:00

I am new to WCF. I was able to successfully create a client for

  • 0

I am new to WCF. I was able to successfully create a client for the GeoNames service, but now that I am trying to do the same for Yahoo GeoPlanet, I can’t seem to get the XML to deserialize into my DataContract types. What is the right way to do this? Here is what I am working with:

Sample REST response:

<places xmlns="http://where.yahooapis.com/v1/schema.rng" 
    xmlns:yahoo="http://www.yahooapis.com/v1/base.rng" 
    yahoo:start="0" yahoo:count="247" yahoo:total="247">
    <place yahoo:uri="http://where.yahooapis.com/v1/place/23424966" 
        xml:lang="en-US">
        <woeid>23424966</woeid>
        <placeTypeName code="12">Country</placeTypeName>
        <name>Sao Tome and Principe</name>
    </place>
    <place yahoo:uri="http://where.yahooapis.com/v1/place/23424824" 
        xml:lang="en-US">
        <woeid>23424824</woeid>
        <placeTypeName code="12">Country</placeTypeName>
        <name>Ghana</name>
    </place>
    ...
</places>

Contract Interface & Client:

[ServiceContract]
public interface IConsumeGeoPlanet
{
    [OperationContract]
    [WebGet(
        UriTemplate = "countries?appid={appId}",
        ResponseFormat = WebMessageFormat.Xml,
        BodyStyle = WebMessageBodyStyle.Bare
    )]
    GeoPlanetResults<GeoPlanetPlace> Countries(string appId);
}

public sealed class GeoPlanetConsumer : ClientBase<IConsumeGeoPlanet>
{
    public GeoPlanetResults<GeoPlanetPlace> Countries(string appId)
    {
        return Channel.Countries(appId);
    }
}

Deserialization Types:

[DataContract(Name = "places", 
    Namespace = "http://where.yahooapis.com/v1/schema.rng")]
public sealed class GeoPlanetResults<T> : IEnumerable<T>
{
    public List<T> Items { get; set; }

    public IEnumerator<T> GetEnumerator()
    {
        return Items.GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }
}


[DataContract]
public class GeoPlanetPlace
{
    [DataMember(Name = "woeid")]
    public int WoeId { get; set; }

    [DataMember(Name = "placeTypeName")]
    public string Type { get; set; }

    [DataMember(Name = "name")]
    public string Name { get; set; }

}

I know this is wrong. In my geonames client, my GeoNamesResults class has a [DataContract] attribute with no properties, and a [DataMember(Name = "geonames")] attribute on the Items property. This doesn’t work for GeoPlanet though, I kept getting deserialization exceptions. The only way I could get the Countries(appId) method to execute without exceptions was by putting the Name and Namespace in the DataContract attribute. However when I do this, I have no idea how to get the results deserialized into the Items collection (it is null).

What should I do?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T18:48:37+00:00Added an answer on May 26, 2026 at 6:48 pm

    The DataContractSerializer doesn’t support the full XML specification, only a subset of it. On thing it doesn’t support is attributes, which are used extensively in the sample response you showed. In this case, you’ll need to use the XmlSerializer, and define the types accordingly (using the attributes in System.Xml.Serialization, instead of the ones on System.Runtime.Serialization). The code below shows how to retrieve the sample XML you posted.

    public class StackOverflow_8022154
    {
        const string XML = @"<places xmlns=""http://where.yahooapis.com/v1/schema.rng""  
        xmlns:yahoo=""http://www.yahooapis.com/v1/base.rng""  
        yahoo:start=""0"" yahoo:count=""247"" yahoo:total=""247""> 
        <place yahoo:uri=""http://where.yahooapis.com/v1/place/23424966""  
            xml:lang=""en-US""> 
            <woeid>23424966</woeid> 
            <placeTypeName code=""12"">Country</placeTypeName> 
            <name>Sao Tome and Principe</name> 
        </place> 
        <place yahoo:uri=""http://where.yahooapis.com/v1/place/23424824""  
            xml:lang=""en-US""> 
            <woeid>23424824</woeid> 
            <placeTypeName code=""12"">Country</placeTypeName> 
            <name>Ghana</name> 
        </place> 
    </places>";
    
        const string ElementsNamespace = "http://where.yahooapis.com/v1/schema.rng";
        const string YahooNamespace = "http://www.yahooapis.com/v1/base.rng";
        const string XmlNamespace = "http://www.w3.org/XML/1998/namespace";
    
        [XmlType(Namespace = ElementsNamespace, TypeName = "places")]
        [XmlRoot(ElementName = "places", Namespace = ElementsNamespace)]
        public class Places
        {
            [XmlAttribute(AttributeName = "start", Namespace = YahooNamespace)]
            public int Start { get; set; }
            [XmlAttribute(AttributeName = "count", Namespace = YahooNamespace)]
            public int Count;
            [XmlAttribute(AttributeName = "total", Namespace = YahooNamespace)]
            public int Total;
            [XmlElement(ElementName = "place", Namespace = ElementsNamespace)]
            public List<Place> AllPlaces { get; set; }
    
            public override string ToString()
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendFormat("Places[start={0},count={1},total={2}]:", this.Start, this.Count, this.Total);
                sb.AppendLine();
                foreach (var place in this.AllPlaces)
                {
                    sb.AppendLine("   " + place.ToString());
                }
    
                return sb.ToString();
            }
        }
        [XmlType(TypeName = "place", Namespace = ElementsNamespace)]
        public class Place
        {
            [XmlAttribute(AttributeName = "uri", Namespace = YahooNamespace)]
            public string Uri { get; set; }
            [XmlAttribute(AttributeName = "lang", Namespace = XmlNamespace)]
            public string Lang { get; set; }
            [XmlElement(ElementName = "woeid")]
            public string Woeid { get; set; }
            [XmlElement(ElementName = "placeTypeName")]
            public PlaceTypeName PlaceTypeName;
            [XmlElement(ElementName = "name")]
            public string Name { get; set; }
    
            public override string ToString()
            {
                return string.Format("Place[Uri={0},Lang={1},Woeid={2},PlaceTypeName={3},Name={4}]",
                    this.Uri, this.Lang, this.Woeid, this.PlaceTypeName, this.Name);
            }
        }
        [XmlType(TypeName = "placeTypeName", Namespace = ElementsNamespace)]
        public class PlaceTypeName
        {
            [XmlAttribute(AttributeName = "code")]
            public string Code { get; set; }
            [XmlText]
            public string Value { get; set; }
    
            public override string ToString()
            {
                return string.Format("TypeName[Code={0},Value={1}]", this.Code, this.Value);
            }
        }
        [ServiceContract]
        public interface IConsumeGeoPlanet
        {
            [OperationContract]
            [WebGet(
                UriTemplate = "countries?appid={appId}",
                ResponseFormat = WebMessageFormat.Xml,
                BodyStyle = WebMessageBodyStyle.Bare
            )]
            [XmlSerializerFormat]
            Places Countries(string appId);
        }
    
        public sealed class GeoPlanetConsumer : ClientBase<IConsumeGeoPlanet>
        {
            public GeoPlanetConsumer(string address)
                : base(new WebHttpBinding(), new EndpointAddress(address))
            {
                this.Endpoint.Behaviors.Add(new WebHttpBehavior());
            }
    
            public Places Countries(string appId)
            {
                return Channel.Countries(appId);
            }
        }
    
        [ServiceContract]
        public class SimulatedYahooService
        {
            [WebGet(UriTemplate = "*")]
            public Stream GetData()
            {
                WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml";
                return new MemoryStream(Encoding.UTF8.GetBytes(XML));
            }
        }
    
        public static void Test()
        {
            Console.WriteLine("First a simpler test with serialization only.");
            XmlSerializer xs = new XmlSerializer(typeof(Places));
            MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(XML));
            object o = xs.Deserialize(ms);
            Console.WriteLine(o);
    
            Console.WriteLine();
            Console.WriteLine("Now in a real service");
            Console.WriteLine();
            string baseAddress = "http://" + Environment.MachineName + ":8000/Service";
            WebServiceHost host = new WebServiceHost(typeof(SimulatedYahooService), new Uri(baseAddress));
            host.Open();
            Console.WriteLine("Host opened");
    
            GeoPlanetConsumer consumer = new GeoPlanetConsumer(baseAddress);
            Places places = consumer.Countries("abcdef");
            Console.WriteLine(places);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

New to WCF, but familiar with COM+ - can I wrap a WCF service
I just referenced my application domain layer dll to the new WCF service that
I am wanting to create an application in winforms that calls a new WCF
I am trying to create two WCF services which should be able to access
I am new to both WSE and WCF and I am trying to consume
When creating a new service in a WCF service project, Visual Studio will automatically
Greetings!! I am new to wcf and have some doubts. I am able to
The brand-new WCF-based code needs (in the meanwhile) to provide a service to a
I am very new to WCF and SOAP messaging but I have managed to
I created WCF service that returns a custom object called XmlElementTreeNode. This is what

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.