Model for xmlconverter:
[XmlRoot(ElementName = "location", IsNullable = true)]
public class location
{
public string city { get; set; }
public string country { get; set; }
public string street { get; set; }
public string postalcode { get; set; }
[XmlElement(ElementName = "geo:point")]
public geoLocation geo { get; set; }
}
[XmlRoot(ElementName = "geo:point", Namespace="http://www.w3.org/2003/01/geo/wgs84_pos#")]
public class geoLocation
{
[XmlElement(ElementName = "geo:lat", Namespace="http://www.w3.org/2003/01/geo/wgs84_pos#")]
public string lat { get; set; }
[XmlElement(ElementName = "geo:long", Namespace = "http://www.w3.org/2003/01/geo/wgs84_pos#")]
public string lon { get; set; }
}
xml:
<location>
<city>Moscow</city>
<country>Russian Federation</country>
<street></street>
<postalcode>236000</postalcode>
<geo:point>
<geo:lat>54.727483</geo:lat>
<geo:long>20.501132</geo:long>
</geo:point>
</location>
Location is ok, but geo – not. What should I do?
I tried to erase namespaces and there’s no changes
You’ve got two problems here. One, as mentioned by Peter Aron Zentai is that you need to apply the namespace at the property level.
XmlRootonly has an effect if it is placed on the root object (which in your case is location).The second problem is that by including the prefix in the element name, you are actually saying you have an element called “geo:point”. What you should be saying is you have an element named “point”, in the namespace “http://www.w3.org/2003/01/geo/wgs84_pos#”, which has the prefix “geo”.
To correct the first problem – simply move the namespace specifier from XmlRoot, to the property itself. To correct the second, remove the prefix from the element name, and set up the namespaces correctly on the serializer, with a prefix of “geo”: