[英]How to get xml node value by using xelement in c#
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 如下所示,我想读取一个名为
1526726702
的节点。所以这个特定的节点集合片段,我有列和行结构形式的数据结构。即列由<measTypes>
标签表示,行由<measResults>
在<measValue>
下。所以我有很多单元格,如“Local Cell ID=10”、“Local Cell ID=11”、“Local Cell ID=12”等。现在我的目标是读取名为1526726740
的列和1526728300
在<measTypes>
部分下获取第一个单元格“本地单元格 ID = 10”的值为43.596
和390824
像这样,我们有很多行用于特定列。我如何读取并获取这些值。 XML SNIPPET 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)
{
}
}
}
}
Your code has a default namespace xmlns="measCollec" So use code below which uses a dictionary您的代码有一个默认命名空间 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());
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.