我应该建立什么类来帮助我反序列化这个xml,windows phone?

时间:2014-05-03 11:55:47

标签: c# xml windows-phone-7 deserialization

我应该构建什么类来帮助我在Windows Phone 7.1中反序列化这个xml?

xml是这样的:

<GeocodeResponse>
<status>OK</status>
<result>
<type>route</type>
<formatted_address>Bear Creek Parkway, Redmond, WA 98052, USA</formatted_address>
<address_component>
<long_name>Bear Creek Parkway</long_name>
<short_name>Bear Creek Pkwy</short_name>
<type>route</type>
</address_component>
<address_component>
<long_name>Downtown</long_name>
<short_name>Downtown</short_name>
<type>neighborhood</type>
<type>political</type>
</address_component>
<address_component>
<long_name>Redmond</long_name>
<short_name>Redmond</short_name>
<type>locality</type>
<type>political</type>
</address_component>
<address_component>
<long_name>King County</long_name>
<short_name>King County</short_name>
<type>administrative_area_level_2</type>
<type>political</type>
</address_component>
<address_component>
<long_name>Washington</long_name>
<short_name>WA</short_name>
<type>administrative_area_level_1</type>
<type>political</type>
</address_component>
<address_component>
<long_name>United States</long_name>
<short_name>US</short_name>
<type>country</type>
<type>political</type>
</address_component>
<address_component>
<long_name>98052</long_name>
<short_name>98052</short_name>
<type>postal_code</type>
</address_component>
<geometry>
<location>
<lat>47.6695537</lat>
<lng>-122.1241124</lng>
</location>
<location_type>APPROXIMATE</location_type>
<viewport>
<southwest>
<lat>47.6682316</lat>
<lng>-122.1253234</lng>
</southwest>
<northeast>
<lat>47.6709296</lat>
<lng>-122.1226255</lng>
</northeast>
</viewport>
<bounds>
<southwest>
<lat>47.6690577</lat>
<lng>-122.1241803</lng>
</southwest>
<northeast>
<lat>47.6701035</lat>
<lng>-122.1237686</lng>
</northeast>
</bounds>
</geometry>
</result>
</GeocodeResponse>

我需要反序列化这个xml以获取长名称和短名称,请帮助我们:(我不知道。

2 个答案:

答案 0 :(得分:1)

XSD工具可用于将XML文档转换为POCO。

对于原始XML,这是一个两步过程。该命令应该从Visual Studio命令提示符运行 - 为方便起见

1. xsd c:\test.xml  /outputdir:folderPath  (this generates test.xsd)
2. xsd test.xsd /classes  /outputdir:folderPath   (this generates the test.cs) 

答案 1 :(得分:0)

您可以使用XmlSerializer.Deserialize方法将其反序列化为动态变量。一旦将其反序列化为动态变量,您就可以通过以下方式引用它:

myDynamicVar.long_name