1

I have an XML as shown below where i want to read one node named 1526726702.So this particular node collection snippet ,i have data structure in form of Columns and rows structure.ie columns are represented by <measTypes> tag and rows are represented by <measResults> coming under <measValue>.So i have many cells like 'Local Cell ID=10','Local Cell ID=11','Local Cell ID=12' etc. Now my aim is to read column named 1526726740 and 1526728300 under section <measTypes> to get values as 43.596 and 390824 for first cell 'Local Cell ID=10'.Like this we have many rows for particular column.How can i read and get this values. XML SNIPPET

<?xml version="1.0" encoding="UTF-8"?>
<measCollecFile xmlns="measCollec" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="Schedule.xsd">
  <fileHeader fileFormatVersion="V7.2" testername="tea">
    <fileSender elementType="SBT900"/>
    <measCollec beginTime="2021-01-24T00:00:00+04:00"/>
  </fileHeader>
  <measData>
    <managedElement userLabel="eelaBldg_474"/>
    <measInfo measInfoId="726702">
    </measInfo>
    <measInfo measInfoId="1526">
    </measInfo>
    <measInfo measInfoId="1526726702">
      <granPeriod duration="PT3600S" endTime="2021-01-24T01:00:00+04:00"/>
      <repPeriod duration="PT3600S"/>
      <measTypes>1526726737 1526726740 1526727483 1526728299 1526728300 1526728301 1526728302 1526728303 1526728304 1526728305  </measTypes>
      <measValue measObjLdn="eelaBldg_474/Cell:eNodeB Function Name=eelaBldg_474, Local Cell ID=10, Cell Name=GhaleelaBldg_A_F1U, eNodeB ID=956474, Cell FDD TDD indication=CELL_TDD">
        <measResults>41.699 43.596 9.241 2461846 390824 27358 0 1263996 5282350 7509028 </measResults>
      </measValue>
      <measValue measObjLdn="eelaBldg_474/Cell:eNodeB Function Name=eelaBldg_474, Local Cell ID=11, Cell Name=GhaleelaBldg_A_F1U, eNodeB ID=956474, Cell FDD TDD indication=CELL_TDD">
        <measResults>42.699 46.596 9.241 2461846 390829 27358 0 1263996 5282350 7509028 </measResults>
      </measValue>
      <measValue measObjLdn="eelaBldg_474/Cell:eNodeB Function Name=eelaBldg_474, Local Cell ID=12, Cell Name=GhaleelaBldg_A_F1U, eNodeB ID=956474, Cell FDD TDD indication=CELL_TDD">
        <measResults>43.699 49.596 9.241 2461846 390826 27358 0 1263996 5282350 7509028 </measResults>
      </measValue>
      
    </measInfo>
  </measData>
</measCollecFile>

code i tried is as below

 using (XmlReader xr = XmlReader.Create(path))
                {
                    xr.MoveToContent();
                    while (xr.Read())
                    {
                        while (xr.NodeType == XmlNodeType.Element && xr.LocalName == "measInfo" && xr.GetAttribute("measInfoId") == "1526726702")
                        {
                            try
                            {
                                XElement pin = (XElement)XNode.ReadFrom(xr);
                                string earfcndl = Getvalue(pin, "measTypes");
                               // string t = pin.Element("measTypes").Value;
    
                                var data = from atts in pin.Elements("measInfo")
                                           select new
                                           {
                                               meas = (string)atts.Element("measTypes")
                                           };
                                string measTypes = data.First().meas;
                            }
                            catch (Exception ex)
                            {
    
                            }
                        }
                    }
                }

1 Answer 1

1

Your code has a default namespace xmlns="measCollec" So use code below which uses a dictionary

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

namespace ConsoleApplication181
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);
            XElement measCollecFile = doc.Root;
            XNamespace ns = measCollecFile.GetDefaultNamespace();

            Dictionary<string,XElement> measInfoDict = measCollecFile.Descendants(ns + "measInfo")
                .GroupBy(x => (string)x.Attribute("measInfoId"), y => y)
                .ToDictionary(x => x.Key, y => y.FirstOrDefault());

            XElement m726702 = (XElement)measInfoDict["1526726702"];

            int numberColumns = ((string)m726702.Descendants(ns + "measResults").FirstOrDefault()).Trim().Split(new char[] { ' ' }).Length;

            string[] strcolumnNames = { "Building", "Cell", "Function Name", "Local Cell ID", "Cell Name", "eNodeB ID", "Cell FDD TDD indication" };
            DataColumn[] strColumns = strcolumnNames.Select(x => new DataColumn(x, typeof(string))).ToArray();

            DataColumn[] columns = Enumerable.Range(0, numberColumns).Select((x, i) => new DataColumn("Col " + (i + 1).ToString(), typeof(decimal))).ToArray();

            DataTable dt = new DataTable();
            dt.Columns.AddRange(strColumns);
            dt.Columns.AddRange(columns);

            foreach (XElement measValue in m726702.Descendants(ns + "measValue"))
            {
                string measObjLdn = (string)measValue.Attribute("measObjLdn");
                int firstSpace = measObjLdn.IndexOf(' ');
                string buildCell = measObjLdn.Substring(0, firstSpace);
                string build = buildCell.Split(new char[] { '/' }).FirstOrDefault();
                string cell = buildCell.Split(new char[] { ':' }).LastOrDefault();
                string parameters = measObjLdn.Substring(firstSpace).Trim();
                string[] parametersValues = parameters.Split(new char[] { ',' }).Select(x => x.Substring(x.IndexOf("=") + 1).Trim()).ToArray();
                parametersValues = (new string[] { build, cell }).Concat(parametersValues).ToArray();
                string values = ((string)measValue.Element(ns + "measResults")).Trim();
                var splitValues = values.Split(new char[] { ' ' }).Select(x => (object)decimal.Parse(x));
                dt.Rows.Add(parametersValues.Concat(splitValues).ToArray());

            }

        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

i achieved this by using XNode.ReadFrom method.But what i want is to get values for columns '1526726740' and '1526728300'.For example if we take first cell 'Local Cell ID=10' , we need to get value as '43.596' and '390824'.Like this many cells i have to take columns '1526726740' and '1526728300' values
it worked like charm.One more thing how can i get value for 'measObjLdn' of 'measValue '

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.