2012-03-28 3 views
0

모델 :XmlDeserialization : 점 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> 

위치 확인하지만, 지리적 - 없습니다. 어떻게해야합니까? 나는 네임 스페이스를 삭제하기 위해 노력하고 XmlElement 속성 정의의 일부로 변경

+0

지리적 뭐가 문제 - 당신이 볼 것으로 예상 했습니까? –

답변

1

여기에는 두 가지 문제가 있습니다. 하나는 Peter Aron Zentai가 언급했듯이 속성 수준에서 네임 스페이스를 적용해야한다는 것입니다. XmlRoot은 루트 개체 (귀하의 경우 위치)에 배치 된 경우에만 효과가 있습니다.

두 번째 문제는 요소 이름에 접두사를 포함하면 실제로 "geo : point"라는 요소가 있다는 것입니다. 당신이 말하고자하는 것은 접두어가 "geo"인 네임 스페이스 "http://www.w3.org/2003/01/geo/wgs84_pos#"에 "point"라는 요소가 있다는 것입니다.

첫 번째 문제를 해결하려면 - 단순히 XmlRoot에서 네임 스페이스 지정자를 속성 자체로 이동하십시오. 두 번째를 수정 요소 이름에서 접두사를 제거하고 "지오"의 접두사로, 시리얼 라이저에 올바르게 네임 스페이스를 설정하려면 :

[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

놓습니다 location 클래스의 geoLocation 유형의 XML - 네임 스페이스가 없습니다. 이 경우 XmlRoot가 적용되지 않습니다!

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