如何在c#中使用xelement获取xml节点值

How to get xml node value by using xelement in c#

我有一个 XML 如下所示,我想读取一个名为 1526726702 的节点。所以这个特定的节点集合片段,我有列和行形式的数据结构 structure.ie 列由 <measTypes> 标记表示,行由 <measValue> 下的 <measResults> 表示。所以我有很多像 'Local Cell ID=10'、'Local Cell ID=11'、'Local Cell ID=12' 等。现在我的目标是阅读 <measTypes> 部分下名为 15267267401526728300 的列,以获取第一个单元格 43.596390824 的值 'Local Cell ID=10'。像这样,我们有很多行用于特定的 column.How 我可以读取并获取这些值吗? XML 片段

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

我试过的代码如下

 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)
                            {
    
                            }
                        }
                    }
                }

你的代码有一个默认命名空间 xmlns="measCollec" 所以使用下面使用字典的代码

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());

            }

        }
    }
}