在 C# 中反序列化多个具有相同名称的 XML 元素
Deserialize multiple XML elements with the same name in C#
我正在尝试解析此 XML 响应:
<?xml version="1.0" encoding="utf-16"?>
<CurrentWeather>
<Location>BOSTON LOGAN INTERNATIONAL, MA, United States (KBOS) 42-22N 071-01W 54M</Location>
<Time>Feb 11, 2015 - 11:54 AM EST / 2015.02.11 1654 UTC</Time>
<Wind> from the N (010 degrees) at 17 MPH (15 KT) gusting to 26 MPH (23 KT):0</Wind>
<Visibility> 2 mile(s):0</Visibility>
<SkyConditions> overcast</SkyConditions>
<Temperature> 19.9 F (-6.7 C)</Temperature>
<Wind>Windchill: 5 F (-15 C):1</Wind>
<DewPoint> 12.9 F (-10.6 C)</DewPoint>
<RelativeHumidity> 73%</RelativeHumidity>
<Pressure> 30.08 in. Hg (1018 hPa)</Pressure>
<Status>Success</Status>
</CurrentWeather>
如您所见,"Wind" 元素出现了两次,这让我很为难,因为我显然不能保存 2 个同名变量。
我尝试通过对元素编号来解决这个问题,我的反序列化 class 现在看起来像这样:
public class CurrentWeather
{
[XmlElement(Order = 1)]
public string Location { get; set; }
[XmlElement(Order = 2)]
public string Time { get; set; }
[XmlElement(Order = 3)]
public string Wind { get; set; }
[XmlElement(Order = 4)]
public string Visibility { get; set; }
[XmlElement(Order = 5)]
public string SkyConditions { get; set; }
[XmlElement(Order = 6)]
public string Temperature { get; set; }
[XmlElement(Order = 7)]
public string WindTemperature { get; set; }
[XmlElement(Order = 8)]
public string DewPoint { get; set; }
[XmlElement(Order = 9)]
public string RelativeHumidity { get; set; }
[XmlElement(Order = 10)]
public string Pressure { get; set; }
[XmlElement(Order = 11)]
public string Status { get; set; }
}
现在问题还在..
它反序列化所有元素,直到第二个 "Wind" 正确,在第二个 "Wind" 之后(包括)其余元素为 'null'
有什么建议吗?
这行得通,只需将 wind 设为数组即可:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WindDeserialize
{
using System.IO;
using System.Xml.Serialization;
class Program
{
public const string xml = "<?xml version=\"1.0\" encoding=\"utf-16\"?> "+
"<CurrentWeather> "+
" <Location>BOSTON LOGAN INTERNATIONAL, MA, United States (KBOS) 42-22N 071-01W 54M</Location> "+
" <Time>Feb 11, 2015 - 11:54 AM EST / 2015.02.11 1654 UTC</Time> "+
" <Wind> from the N (010 degrees) at 17 MPH (15 KT) gusting to 26 MPH (23 KT):0</Wind> "+
" <Visibility> 2 mile(s):0</Visibility> "+
" <SkyConditions> overcast</SkyConditions> "+
" <Temperature> 19.9 F (-6.7 C)</Temperature> "+
" <Wind>Windchill: 5 F (-15 C):1</Wind> "+
" <DewPoint> 12.9 F (-10.6 C)</DewPoint> "+
" <RelativeHumidity> 73%</RelativeHumidity> "+
" <Pressure> 30.08 in. Hg (1018 hPa)</Pressure> "+
" <Status>Success</Status> "+
"</CurrentWeather> ";
static void Main(string[] args)
{
XmlSerializer ser = new XmlSerializer(typeof(CurrentWeather));
var weather = (CurrentWeather)ser.Deserialize(new StringReader(xml));
}
}
public class CurrentWeather
{
[XmlElement]
public string Location { get; set; }
[XmlElement]
public string Time { get; set; }
[XmlElement]
public string [] Wind { get; set; }
[XmlElement]
public string Visibility { get; set; }
[XmlElement]
public string SkyConditions { get; set; }
[XmlElement]
public string Temperature { get; set; }
[XmlElement]
public string DewPoint { get; set; }
[XmlElement]
public string RelativeHumidity { get; set; }
[XmlElement]
public string Pressure { get; set; }
[XmlElement]
public string Status { get; set; }
}
}
我正在尝试解析此 XML 响应:
<?xml version="1.0" encoding="utf-16"?>
<CurrentWeather>
<Location>BOSTON LOGAN INTERNATIONAL, MA, United States (KBOS) 42-22N 071-01W 54M</Location>
<Time>Feb 11, 2015 - 11:54 AM EST / 2015.02.11 1654 UTC</Time>
<Wind> from the N (010 degrees) at 17 MPH (15 KT) gusting to 26 MPH (23 KT):0</Wind>
<Visibility> 2 mile(s):0</Visibility>
<SkyConditions> overcast</SkyConditions>
<Temperature> 19.9 F (-6.7 C)</Temperature>
<Wind>Windchill: 5 F (-15 C):1</Wind>
<DewPoint> 12.9 F (-10.6 C)</DewPoint>
<RelativeHumidity> 73%</RelativeHumidity>
<Pressure> 30.08 in. Hg (1018 hPa)</Pressure>
<Status>Success</Status>
</CurrentWeather>
如您所见,"Wind" 元素出现了两次,这让我很为难,因为我显然不能保存 2 个同名变量。
我尝试通过对元素编号来解决这个问题,我的反序列化 class 现在看起来像这样:
public class CurrentWeather
{
[XmlElement(Order = 1)]
public string Location { get; set; }
[XmlElement(Order = 2)]
public string Time { get; set; }
[XmlElement(Order = 3)]
public string Wind { get; set; }
[XmlElement(Order = 4)]
public string Visibility { get; set; }
[XmlElement(Order = 5)]
public string SkyConditions { get; set; }
[XmlElement(Order = 6)]
public string Temperature { get; set; }
[XmlElement(Order = 7)]
public string WindTemperature { get; set; }
[XmlElement(Order = 8)]
public string DewPoint { get; set; }
[XmlElement(Order = 9)]
public string RelativeHumidity { get; set; }
[XmlElement(Order = 10)]
public string Pressure { get; set; }
[XmlElement(Order = 11)]
public string Status { get; set; }
}
现在问题还在.. 它反序列化所有元素,直到第二个 "Wind" 正确,在第二个 "Wind" 之后(包括)其余元素为 'null' 有什么建议吗?
这行得通,只需将 wind 设为数组即可:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WindDeserialize
{
using System.IO;
using System.Xml.Serialization;
class Program
{
public const string xml = "<?xml version=\"1.0\" encoding=\"utf-16\"?> "+
"<CurrentWeather> "+
" <Location>BOSTON LOGAN INTERNATIONAL, MA, United States (KBOS) 42-22N 071-01W 54M</Location> "+
" <Time>Feb 11, 2015 - 11:54 AM EST / 2015.02.11 1654 UTC</Time> "+
" <Wind> from the N (010 degrees) at 17 MPH (15 KT) gusting to 26 MPH (23 KT):0</Wind> "+
" <Visibility> 2 mile(s):0</Visibility> "+
" <SkyConditions> overcast</SkyConditions> "+
" <Temperature> 19.9 F (-6.7 C)</Temperature> "+
" <Wind>Windchill: 5 F (-15 C):1</Wind> "+
" <DewPoint> 12.9 F (-10.6 C)</DewPoint> "+
" <RelativeHumidity> 73%</RelativeHumidity> "+
" <Pressure> 30.08 in. Hg (1018 hPa)</Pressure> "+
" <Status>Success</Status> "+
"</CurrentWeather> ";
static void Main(string[] args)
{
XmlSerializer ser = new XmlSerializer(typeof(CurrentWeather));
var weather = (CurrentWeather)ser.Deserialize(new StringReader(xml));
}
}
public class CurrentWeather
{
[XmlElement]
public string Location { get; set; }
[XmlElement]
public string Time { get; set; }
[XmlElement]
public string [] Wind { get; set; }
[XmlElement]
public string Visibility { get; set; }
[XmlElement]
public string SkyConditions { get; set; }
[XmlElement]
public string Temperature { get; set; }
[XmlElement]
public string DewPoint { get; set; }
[XmlElement]
public string RelativeHumidity { get; set; }
[XmlElement]
public string Pressure { get; set; }
[XmlElement]
public string Status { get; set; }
}
}