简体   繁体   中英

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

I would like to get a list of the element <Events> of the following XML using LINQ to XML if it's possible:

<?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>

I have a class called Event . I would like to create an instance of each event filling the attributes of the event class with the attributes inside the element Events :

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

}

Try following :

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


            }
        }

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM