c# .net XML序列化从XML元素数组中检索值
本文关键字:XML 检索 数组 net 序列化 元素 | 更新日期: 2023-09-27 18:13:25
我有以下示例xml,我试图反序列化xml以从xml元素数组("READING"元素)获得值,以获得下面的"22.9"值(很难解释)。
我可以使用XmlSerializer获得所有其他值。
xml示例:
<LIVE deviceid="1394602" utc="18-Oct-2016 01:22:28" local="18-Oct-2016 12:22:28">
<READINGS type="full" probecount="1" portcount="2" discovery="2">
<READING parameter="Temperature" shortparameter="Temp" unit="°C" channel="1" probeid="3" state="OK" probename="EC202" sensor="0">22.9</READING>
<READING parameter="Ozone" shortparameter="O3" unit="ppm" channel="0" probeid="3" state="OK" probename="EC202" sensor="72">5</READING>
</READINGS>
</LIVE>
我能得到帮助吗?
谢谢
class Program
{
static void Main(string[] args)
{
const string xml = @"<LIVE deviceid=""1394602"" utc=""18-Oct-2016 01:22:28"" local=""18-Oct-2016 12:22:28"">
<READINGS type=""full"" probecount=""1"" portcount=""2"" discovery=""2"">
<READING parameter=""Temperature"" shortparameter=""Temp"" unit=""°C"" channel=""1"" probeid=""3"" state=""OK"" probename=""EC202"" sensor=""0"">**22.9**
</READING>
</READINGS>
</LIVE>";
var serializer = new XmlSerializer(typeof(LIVE));
using(var reader = new StringReader(xml))
{
var live = (LIVE)serializer.Deserialize(reader);
var output = default(double);
var text = Regex.Match(live.READINGS.READING.Text, @"'d+('.'d+)?").Value;
if (double.TryParse(text, out output))
{
Console.WriteLine(output);
}
}
Console.ReadLine();
}
}
[XmlRoot(ElementName = "READING")]
class READING
{
[XmlAttribute(AttributeName = "parameter")]
public string Parameter { get; set; }
[XmlAttribute(AttributeName = "shortparameter")]
public string Shortparameter { get; set; }
[XmlAttribute(AttributeName = "unit")]
public string Unit { get; set; }
[XmlAttribute(AttributeName = "channel")]
public string Channel { get; set; }
[XmlAttribute(AttributeName = "probeid")]
public string Probeid { get; set; }
[XmlAttribute(AttributeName = "state")]
public string State { get; set; }
[XmlAttribute(AttributeName = "probename")]
public string Probename { get; set; }
[XmlAttribute(AttributeName = "sensor")]
public string Sensor { get; set; }
[XmlText]
public string Text { get; set; }
}
[XmlRoot(ElementName = "READINGS")]
class READINGS
{
[XmlElement(ElementName = "READING")]
public READING READING { get; set; }
[XmlAttribute(AttributeName = "type")]
public string Type { get; set; }
[XmlAttribute(AttributeName = "probecount")]
public string Probecount { get; set; }
[XmlAttribute(AttributeName = "portcount")]
public string Portcount { get; set; }
[XmlAttribute(AttributeName = "discovery")]
public string Discovery { get; set; }
}
[XmlRoot(ElementName = "LIVE")]
class LIVE
{
[XmlElement(ElementName = "READINGS")]
public READINGS READINGS { get; set; }
[XmlAttribute(AttributeName = "deviceid")]
public string Deviceid { get; set; }
[XmlAttribute(AttributeName = "utc")]
public string Utc { get; set; }
[XmlAttribute(AttributeName = "local")]
public string Local { get; set; }
}