2014-02-18 4 views
1

내 XML :RestSharp에서 xml을 deserialize하여 목록에 추가하는 방법은 무엇입니까?

<result> 
    <document version="2.1.0"> 
     <response type="currency"> 
      <currency> 
       <code>AMD</code> 
       <price>85.1366</price> 
      </currency> 
     </response> 
     <response type="currency"> 
      <currency> 
       <code>AUD</code> 
       <price>31.1207</price> 
      </currency> 
     </response> 
    </document> 
</result> 

나의 등급 :

public class CurrencyData 
{ 
    public string Code { get; set; } 
    public string Price { get; set; } 
} 

내 디시리얼라이저 전화 :

RestClient.ExecuteAsync<List<CurrencyData>>... 

내가 모든 잘 수행 된 것이다 Currency에 클래스 CurrencyData 이름을 변경 한 경우. 하지만이 수업 명을 갖고 싶습니다.

+0

은'DeserializeAs' 속성이 단일 항목에서 제대로 작동에서입니다. 문제는 컬렉션에 사용되는 대체 워크 플로 내 Deserializer가 DeserializeAs 특성을 확인했기 때문입니다. 말 그대로 HandleListDerivative 메소드에서 2 ~ 3 줄의 코드로 작업 할 수있었습니다. (저는 Github에서 XmlDeserializer 클래스의 소스를 가져 와서 변경 사항이있는 사용자 정의 디시리얼라이저로 등록했습니다). –

답변

3

가 좋아, 내가 그것을 가지고 생각, 다음은 XML 디시리얼라이저에 대한 문서입니다 소문자로 클래스와 속성의 이름을 지정해야합니다. 케이싱과 일치하도록 XML을 바꿀 수 있다면 그들을 놓을 수 있습니다.

+0

아, 앨리어싱.아래의 요점은 이름이 일치해야하고 RestSharp 가이드가 조회를 수행하는 방법에 대한 요약을 제공한다는 것입니다. – u84six

0

그런 다음 xml 태그를 CurrencyData로 변경하십시오. 당신은 내가없이 케이스를 오버라이드 (override) 할 수있는 몇 XmlElement 속성을 추가했다 RestClient.ExecuteAsync<Result>()

[XmlRoot("result")] 
public class Result 
{ 
    [XmlElement("document")] 
    public Document Document { get; set; } 
} 

public class Document 
{ 
    [XmlElement("response")] 
    public Response[] Responses { get; set; } 

    [XmlAttribute("version")] 
    public string Version { get; set; } 
} 

public class Response 
{ 
    [XmlElement("currency")] 
    public CurrencyData Currency { get; set; } 

    [XmlAttribute("type")] 
    public string Type { get; set; } 
} 

public class CurrencyData 
{ 
    [XmlElement("code")] 
    public string Code { get; set; } 

    [XmlElement("price")] 
    public decimal Price { get; set; } 
} 

시도 할 수 있습니다

https://github.com/restsharp/RestSharp/wiki/Deserialization

0

Kay.one의 대답이 받아 들여지는 이유는 모르겠지만 질문에 답할 수 없습니다.

내 의견에 따르면 기본 RestSharp 디시리얼라이저는 목록을 비 직렬화 할 때 DeserializeAs 특성을 확인하지 않습니다. 저자가 매우 유용하지 않은 것처럼 의도적인지 실수인지 확실하지 않습니다.

어쨌든 간단한 수정입니다.

private object HandleListDerivative(object x, XElement root, string propName, Type type) 
    { 
     Type t; 

     if (type.IsGenericType) 
     { 
      t = type.GetGenericArguments()[0]; 
     } 
     else 
     { 
      t = type.BaseType.GetGenericArguments()[0]; 
     } 

     var list = (IList)Activator.CreateInstance(type); 
     var elements = root.Descendants(t.Name.AsNamespaced(Namespace)); 

     var name = t.Name; 

     //add the following 
     var attribute = t.GetAttribute<DeserializeAsAttribute>(); 
     if (attribute != null) 
      name = attribute.Name; 
+0

어디 넣을까요? 우리가 Xamarin에서 사용하기 때문에 RestSharp 라이브러리를 수정할 필요가 없기를 희망합니다 ... – kenyee

+0

그것이 감독인지 아닌지 확실하지 않습니다. 나는 물건을 조금 벗어났다. 그래서 나는 내가 돌아 왔을 때 그것을 들여다 볼 것이다. – Michael

0

kay.one의 답변은 완벽합니다. 그것은 어떤 발언에서 작동합니다

public List<Response> Responses { get; set; } 

그리고

[XmlElement("AnyValue")] 

는 System.Xml.Serialization 네임 스페이스와 don`에서입니다 작동 돈`t

public Response[] Responses { get; set; } 

작동 일하지 마라. 그냥 삭제하는 것이 좋습니다. 이 예제에서 주석 속성 및 속성은 동일한 이름을 가지며 serializer는이를 인식합니다. 하지만 지금 주석 속성은, 그러나 그것은 모음에서 작동하지 않습니다, RestSharp.Deserializers 네임 스페이스

[DeserializeAs(Name="AnyXmlValue")] 
    public string AnyModelValue { get; set; } 

How to manage deserialization of RestSharp

관련 문제