0

I want xml with custom name for element atrribute, I declared the name as shown below

[XmlRoot(ElementName = "country")]
public class CountryInRegion
{
    [XmlElement(ElementName = "iso_code")]
    public string IsoCode { get; set; }

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

    [XmlElement(ElementName = "region_id")]
    public string RegionId { get; set; }
}

[XmlRoot(ElementName = "countries")]
public class Countries
{
    [XmlArray(ElementName = "country")]
    public IList<CountryInRegion> countries { get; set; }

    public Countries()
    {
        countries = new List<CountryInRegion>();
    }
}

and I get the output like this

<Countries>
 <countries>
  <CountryInRegion>
   <IsoCode>AD</IsoCode>
   <Name>Andorra</Name>
   <RegionId>EUROPE</RegionId></CountryInRegion>
  </CountryInRegion>
 </countries>
</Countries>

i tried [XmlElement(“name")] as well. but no changes is obtained

Sanjeev S
  • 626
  • 1
  • 8
  • 27

1 Answers1

0

depending on how you are generating the xml from the obj, you might want to explicitly declare the class as [Serializable] see What is [Serializable] and when should I use it?. Your renames look fine above.

Community
  • 1
  • 1
BMac
  • 2,183
  • 3
  • 22
  • 30