Viku
Viku

Reputation: 2973

XML parsing to get a particular tag value in a list of XElement

I have below XML and c# code. I want to get the list of values like 333,382 in a list present in <Event >tag.

However with below code i am getting the list of whole Event tag like{ Event = "333" },{ Event = "382" }

enter image description here

I can code few more logic to get only the numeric part out of it but for code efficiency its better to get the required value like 333,382 in eventList itself through LINQ.

Can somebody please help on it ? Thanks in advance.

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

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            string eventListXML= @"<XYZ>
                                    <Task>
                                        <ABC>
                                            <EvtLogResponse>
                                                <LogInfo>
                                                    <CommID>8</CommID>
                                                    <UserId>2</UserId>
                                                    <Date>10/09/2020</Date>
                                                    <Time>06:24:01 PM</Time>
                                                </LogInfo>
                                                <EventLogAllRes>
                                                    <EventOrRestore>E</EventOrRestore>
                                                    <Event>333</Event>
                                                </EventLogAllRes>
                                                <EventLogAllRes>
                                                    <EventOrRestore>E</EventOrRestore>
                                                    <Event>382</Event>
                                                </EventLogAllRes>
                                                <FilePath>C:\Windows\test.txt</FilePath>
                                            </EvtLogResponse>
                                            <UserID>1</UserID>
                                        </ABC>
                                    </Task>
                                </XYZ>";

            XDocument xDoc = XDocument.Parse(eventListXML);
            XElement Xele = xDoc.Element("XYZ").Element("Task").Element("ABC").Element("EvtLogResponse");
            List<XElement> Logs = Xele.Elements("EventLogAllRes").ToList<XElement>();


            var eventList = (from Event in Logs.Descendants("Event")
                             select new
                             {
                                 Event = Event.Value

                             }).ToList();
             
        }
    }
}

Upvotes: 1

Views: 297

Answers (1)

Prince Khanna
Prince Khanna

Reputation: 66

You want it to be like list of integers right??

Just remove make a new Event and code like below

var eventList = (from Event in Logs.Descendants("Event")
                                 select 
                                     Regex.Replace(Event.Value.ToString(), "[^0-9]+", string.Empty)).ToList();

Further if you want to have comma separated values then add below code to the code above.

var result = String.Join(",", eventList);

Here result will have the comma separated string.

Let me know if more anything else is needed.

Upvotes: 1

Related Questions