3

Here is the XML that I'd like the webservice to deliver:

<business>
  <locations>
    <location>location 1</location>
    <location>location 2</location>
  </locations>
</business>

However, instead the following is being returned:

<business>
  <locations>
    <location>
      <name>location 1</name>
    </location>
    <location>
       <name>location 2</name>
    </location>
  </locations>
</business>

Here is the code used:

    [WebMethod, ScriptMethod(ResponseFormat = ResponseFormat.Xml)]
    public Business GetBusiness()
    {
        var business = new Business();
        business.Locations = new List<Location>();
        business.Locations.Add(new Location { Name = "location 1" });
        business.Locations.Add(new Location { Name = "location 2" });
        return business;
    }

    [XmlType(TypeName = "business")]
    public class Business
    {
        [XmlArray(ElementName = "locations")]
        [XmlArrayItem(ElementName = "location")]
        public List<Location> Locations { get; set; }
    }

    [XmlType(TypeName = "location")]
    public class Location
    {
        [XmlElement(ElementName = "name")]
        public string Name { get; set; }
    }

How does one get the location string include the location tag instead of having a name tag?

TIA, George

3 Answers 3

3

You need to use the XmlTextAttribute on the Name member to treat it as the text of an XML element:

[XmlType(TypeName = "location")]
public class Location
{
    [XmlText()]
    public string Name { get; set; }
}
Sign up to request clarification or add additional context in comments.

1 Comment

YES! That is the trick I've been looking for... XmlText works perfectly, thank you.
1

Why not just use a list of strings for locations instead of a list of location objects?

    [WebMethod, ScriptMethod(ResponseFormat = ResponseFormat.Xml)]
    public Business GetBusiness()
    {
        var business = new Business();
        business.Locations = new List<string>();
        business.Locations.Add("location 1");
        business.Locations.Add("location 2");
        return business;
    }

    [XmlType(TypeName = "business")]
    public class Business
    {
        [XmlArray(ElementName = "locations")]
        [XmlArrayItem(ElementName = "location")]
        public List<string> Locations { get; set; }
    }

    //[XmlType(TypeName = "location")]
    //public class Location
    //{
    //    [XmlElement(ElementName = "name")]
    //    public string Name { get; set; }
    //}

That results in the XML you're looking for.

Comments

0

You can control the serialization and deserialization process using a number of methods specified in MSDN: http://msdn.microsoft.com/en-us/library/ty01x675%28v=vs.80%29.aspx

In your case you are serializing a list of locations, using the ElementName "location". If you were serializing a list of strings public List<string> Locations {get; set; } you would get the expected result. The Location object you use places an extra level in the hiearchy, thus is serialized on it's own with it's own properties (like name).

If you implement the ISerializable interface on the Locations class, you will be in full control of the generated XML output, and can simply pass back the contents of your 'Name' property.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.