2016-08-09 12 views
0

これがシリアル化されると、結果のリストにゼロの結果が得られます。私は実際には、結果のジオメトリを取得しようとしていますが、何らかの理由ですべての結果が表示されていないと私は、カウントを行うとき、私はここにxml to c#クラスが正しくシリアル化されない

0は、XMLの抜粋で取得

<?xml version="1.0" encoding="UTF-8"?> 
<PlaceSearchResponse> 
<status>OK</status> 
<result> 
    <name>Premier Inn Manchester Deansgate Locks</name> 
    <vicinity>Medlock Street, Manchester</vicinity> 
    <type>lodging</type> 
    <type>restaurant</type> 
    <type>food</type> 
    <type>point_of_interest</type> 
    <type>establishment</type> 
    <geometry> 
    <location> 
    <lat>53.4713048</lat> 
    <lng>-2.2474693</lng> 
    </location> 
    <viewport> 
    <southwest> 
    <lat>53.4711143</lat> 
    <lng>-2.2475661</lng> 
    </southwest> 
    <northeast> 
    <lat>53.4718764</lat> 
    <lng>-2.2473777</lng> 
    </northeast> 
    </viewport> 
    </geometry> 

C#

if (webResponse.error == null) 
    { 
     print(webResponse.text); 
     PlacesApiQueryResponse placesObject = LoadFromText(webResponse.text); 
     print(placesObject.results.Count); 

     foreach(var entity in placesObject.results) 
     { 
      print(entity.geometry.location.lat + " | " + entity.geometry.location.lng); 
     } 

    } 
    else 
    { 
     print(webResponse.error); 
    } 
} 

public static PlacesApiQueryResponse LoadFromText(string text) 
{ 
    var serializer = new XmlSerializer(typeof(PlacesApiQueryResponse), new XmlRootAttribute("PlaceSearchResponse")); 
    return serializer.Deserialize(new StringReader(text)) as PlacesApiQueryResponse; 
} 
} 

public class Location 
{ 
    public double lat { get; set; } 
    public double lng { get; set; } 
} 

public class Geometry 
{ 
    public Location location { get; set; } 
} 

public class OpeningHours 
{ 
    public bool open_now { get; set; } 
} 

public class Photo 
{ 
    public int height { get; set; } 
    public List<object> html_attributions { get; set; } 
    public string photo_reference { get; set; } 
    public int width { get; set; } 
} 

public class AltId 
{ 
    public string place_id { get; set; } 
    public string scope { get; set; } 
} 

public class Result 
{ 
    public Geometry geometry { get; set; } 
    public string icon { get; set; } 
    public string id { get; set; } 
    public string name { get; set; } 
    public OpeningHours opening_hours { get; set; } 
    public List<Photo> photos { get; set; } 
    public string place_id { get; set; } 
    public string scope { get; set; } 
    public List<AltId> alt_ids { get; set; } 
    public string reference { get; set; } 
    public List<string> types { get; set; } 
    public string vicinity { get; set; } 
    public string rating { get; set; } 
} 

public class PlacesApiQueryResponse 
{ 
    public List<object> html_attributions { get; set; } 
    public List<Result> results { get; set; } 
    public string status { get; set; } 
} 
+0

元のC#コードで 'if(webResponse.error == null)'の前に来たことはありますか?また、あなたは何をシリアライズしていますか?シリアライズはどこですか? –

+0

"PlacesApiQueryResponse"クラスの名前を "PlaceSearchResponse"に変更するか、クラスにXMLElementデコレーションを追加します。 –

+0

@Telansこれは問題ではありません。 –

答えて

1

あなたのクラスは、XmlSerializerの仕組みに関する入力XMLに対応していません。

<list-element> 
    <item-element> 
     … content goes here … 
    </item-element> 
    … 
</list-element> 

あなたは例えば、XMLおよび対応するクラスの両方を変更する必要があります:

<results> ← new element groupping all <result> elements 
    <result>…</result> 
</result> 

とクラスの問題は、このように、2レベルのXMLが必要とされList<T>シリアライズ、です:

public class PlacesApiQueryResponse 
{ 
    public List<object> html_attributions { get; set; } 

    // attribute to tell XmlSerializer how are the item-elements named 
    [XmlArrayItem("result")] 
    public List<Result> results { get; set; } 
    public string status { get; set; } 
} 
+0

私はGoogleの応答を変更することはできません、私は属性を設定することができます。 – user1552172

+0

私はこれが動作することに同意します。私はcan notを変更します。xml :( – user1552172

関連する問題