c#.net xml序列化从xml元素数组中检索值

时间:2016-10-18 07:06:52

标签: c# .net xml xmlserializer

我有以下示例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>

我能为此获得帮助吗?

由于

1 个答案:

答案 0 :(得分:0)

    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; }
    }