2012-03-28 9 views
0

Modell für xmlconverter:XmlDeserialization von geo: Punkt

[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> 

Die Lage ist in Ordnung, aber geo - nicht. Was soll ich machen? Ich habe versucht, Namespaces zu löschen und es gibt keine Änderungen

+0

Was mit geo falsch ist - was haben Sie zu erwarten? –

Antwort

1

Sie haben hier zwei Probleme. Einer, wie von Peter Aron Zentai erwähnt, ist, dass Sie den Namespace auf der Eigenschaftsebene anwenden müssen. hat nur dann einen Effekt, wenn es auf das Root-Objekt gesetzt wird (was in Ihrem Fall der Standort ist).

Das zweite Problem ist, dass Sie, indem Sie das Präfix in den Elementnamen einfügen, sagen, dass Sie ein Element namens "geo: point" haben. Was Sie sagen sollten ist, dass Sie ein Element namens "point" im Namensraum "http://www.w3.org/2003/01/geo/wgs84_pos#" haben, welches das Präfix "geo" hat.

Um das erste Problem zu beheben - einfach den Namespace-Bezeichner aus XmlRoot in die Eigenschaft selbst verschieben. Um die zweiten zu korrigieren, entfernen Sie das Präfix aus dem Elementnamen, und richten Sie die Namespaces korrekt auf dem Serializer mit dem Präfix „geo“:

[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 = "point", Namespace = "http://www.w3.org/2003/01/geo/wgs84_pos#")] 
    public geoLocation geo { get; set; } 
} 

public class geoLocation 
{ 
    [XmlElement(ElementName = "lat", Namespace = "http://www.w3.org/2003/01/geo/wgs84_pos#")] 
    public string lat { get; set; } 
    [XmlElement(ElementName = "long", Namespace = "http://www.w3.org/2003/01/geo/wgs84_pos#")] 
    public string lon { get; set; } 
} 

var serializer = new XmlSerializer(typeof (location)); 
var namespace = new XmlQualifiedName("geo", "http://www.w3.org/2003/01/geo/wgs84_pos#"); 
var namespaces = new XmlSerializerNamespaces(new [] { namespace }); 
serializer.Serialize(myOutputStreamOrWriter, location, namespaces); 
0

Setzen Sie den XML-Namespace des geoLocation Typ in der location Klasse als Teil der XmlElement Attributdefinition. XmlRoot wird in diesem Fall nicht angewendet!

[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 = "point", Namespace="http://www.w3.org/2003/01/geo/wgs84_pos#")] 
    public geoLocation geo { get; set; } 
}