C# 使用 LINQ to XML 将 XML 元素解析为 类 的列表

C# parse XML element to list of classes using LINQ to XML

如果可能的话,我想使用 LINQ to XML 获取以下 XML 的元素 <Events> 的列表:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="/styles/Streams.xsl"?>
<MTConnectStreams
    xmlns:m="urn:data.org:dataStreams:1.3"
    xmlns="urn:data.org:dataStreams:1.3"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:data.org:dataStreams:1.3 \schemas\/dataStreams_1.3.xsd">
    <Header creationTime="2019-01-24T08:55:29Z" sender="3b1c579069c8" instanceId="1548058692" version="1.4.0.10" bufferSize="131072" nextSequence="745" firstSequence="1" lastSequence="744"/>
    <Streams>
        <DeviceStream name="gmb31" uuid="000">
            <ComponentStream component="Path" name="path" componentId="pth">
                <Events>
                    <Execution dataItemId="19" timestamp="2019-01-21T08:18:12.675662Z" name="execution1" sequence="7">STARTED</Execution>
                    <ControllerMode dataItemId="27" timestamp="2019-01-21T08:18:12.675662Z" name="mode1" sequence="12">AUTOMATIC</ControllerMode>
                </Events>
            </ComponentStream>
            <ComponentStream component="Controller" name="controller" componentId="cn1">
                <Events>
                    <Message dataItemId="10" timestamp="2019-01-26T21:05:48.683808Z" name="message" sequence="2">UNAVAILABLE</Message>
                </Events>
            </ComponentStream>
        </DeviceStream>
    </Streams>
</MTConnectStreams>

我有一个叫 Event 的 class。我想创建每个事件的实例,用元素 Events:

中的属性填充事件 class 的属性
public class Event{

    private String dataItemId{ get; set; }

    private String Timestamp{ get; set; }

    private String Name{ get; set; }

    //STARTED AUTOMATIC in the xml file
    private String Value { get; set;}

}

尝试以下操作:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;


namespace ConsoleApplication98
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            Event cEvent = new Event(FILENAME);
        }
    }
    public class Event
    {
        static List<Event> events = new List<Event>();

        private String dataItemId { get; set; }

        private DateTime Timestamp { get; set; }

        private String Name { get; set; }

        //STARTED AUTOMATIC in the xml file
        private String Value { get; set; }

        public Event() { }
        public Event(string filename)
        {
            XDocument doc = XDocument.Load(filename);
            XNamespace ns = doc.Root.GetDefaultNamespace();

            foreach (XElement xEvent in doc.Descendants(ns + "Events"))
            {
                foreach (XElement element in xEvent.Elements())
                {
                    Event newEvent = new Event();
                    events.Add(newEvent);
                    newEvent.dataItemId = (string)element.Attribute("dataItemId");
                    newEvent.Timestamp = (DateTime)element.Attribute("timestamp");
                    newEvent.Name = element.Name.LocalName;
                    newEvent.Value = (string)element;
                }


            }
        }

    }
}