如何根据给定 xml 文件中的前一个节点获取值
How to take value based on previous node from the given xml file
我有一个如下所示的 xml 文件,它是 xml 大文件的一小部分
<?xml version="1.0" encoding="utf-8" ?>
<xn:VsDataContainer id=test">
<xn:attributes>
<xn:vsDataType>vsDataEUtranCellFDD</xn:vsDataType>
<es:crsGain>0</es:crsGain>
<es:pciConflictCell>
<es:enbId>66111</es:enbId>
<es:cellId>3</es:cellId>
</es:pciConflictCell>
<es:pdcchLaGinrMargin>100</es:pdcchLaGinrMargin>
<es:lbEUtranAcceptOffloadThreshold>50</es:lbEUtranAcceptOffloadThreshold>
<es:pdcchCfiMode>5</es:pdcchCfiMode>
<es:cellId>0</es:cellId>
<es:zzzTemporary21>-2000000000</es:zzzTemporary21>
</xn:attributes>
</xn:VsDataContainer>
我正在使用以下代码获取 crsGain
和 cellId
的值。但是 cellId
我没有得到想要的值.. 即我需要 cellId
如果前一个节点是 pdcchCfiMode
。所以在这里我应该得到值 0,但我得到 3,这是序列中的第一个。如何解决这个 issue.Snippet 我正在使用的代码是
if (vsDataEUtranCellFDD.Any()) {
List<CellName> cells = vsDataEUtranCellFDD.Select(x => new CellName() {
cellId= (int)x.Descendants().Where(a => a.Name.LocalName == "cellId").FirstOrDefault(),
crsGain = (int)x.Descendants().Where(a => a.Name.LocalName == "crsGain").FirstOrDefault(),
编辑
这个cellid也可以出现在中间,只是区别是前一个节点是pdcchCfiMode
你可以 skip the elements while 它们不等于 pdcchCfiMode
然后取第一个元素。像这样:
cellId = (int)x.Descendants().SkipWhile(a => a.Name.LocalName != "pdcchCfiMode")
.Skip(1).Take(1).FirstOrDefault(),
试试下面的代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
List<XElement> attributes = doc.Descendants().Where(x => x.Name.LocalName == "attributes").ToList();
XNamespace esNs = attributes.FirstOrDefault().GetNamespaceOfPrefix("es");
List<CellName> cells = attributes.Select(x => new CellName() {
cellId = (int)x.Element(esNs + "cellId"),
crsGain = (int)x.Element(esNs + "crsGain")
}).ToList();
}
}
public class CellName
{
public int cellId { get; set; }
public int crsGain { get; set; }
}
}
我有一个如下所示的 xml 文件,它是 xml 大文件的一小部分
<?xml version="1.0" encoding="utf-8" ?>
<xn:VsDataContainer id=test">
<xn:attributes>
<xn:vsDataType>vsDataEUtranCellFDD</xn:vsDataType>
<es:crsGain>0</es:crsGain>
<es:pciConflictCell>
<es:enbId>66111</es:enbId>
<es:cellId>3</es:cellId>
</es:pciConflictCell>
<es:pdcchLaGinrMargin>100</es:pdcchLaGinrMargin>
<es:lbEUtranAcceptOffloadThreshold>50</es:lbEUtranAcceptOffloadThreshold>
<es:pdcchCfiMode>5</es:pdcchCfiMode>
<es:cellId>0</es:cellId>
<es:zzzTemporary21>-2000000000</es:zzzTemporary21>
</xn:attributes>
</xn:VsDataContainer>
我正在使用以下代码获取 crsGain
和 cellId
的值。但是 cellId
我没有得到想要的值.. 即我需要 cellId
如果前一个节点是 pdcchCfiMode
。所以在这里我应该得到值 0,但我得到 3,这是序列中的第一个。如何解决这个 issue.Snippet 我正在使用的代码是
if (vsDataEUtranCellFDD.Any()) {
List<CellName> cells = vsDataEUtranCellFDD.Select(x => new CellName() {
cellId= (int)x.Descendants().Where(a => a.Name.LocalName == "cellId").FirstOrDefault(),
crsGain = (int)x.Descendants().Where(a => a.Name.LocalName == "crsGain").FirstOrDefault(),
编辑
这个cellid也可以出现在中间,只是区别是前一个节点是pdcchCfiMode
你可以 skip the elements while 它们不等于 pdcchCfiMode
然后取第一个元素。像这样:
cellId = (int)x.Descendants().SkipWhile(a => a.Name.LocalName != "pdcchCfiMode")
.Skip(1).Take(1).FirstOrDefault(),
试试下面的代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
List<XElement> attributes = doc.Descendants().Where(x => x.Name.LocalName == "attributes").ToList();
XNamespace esNs = attributes.FirstOrDefault().GetNamespaceOfPrefix("es");
List<CellName> cells = attributes.Select(x => new CellName() {
cellId = (int)x.Element(esNs + "cellId"),
crsGain = (int)x.Element(esNs + "crsGain")
}).ToList();
}
}
public class CellName
{
public int cellId { get; set; }
public int crsGain { get; set; }
}
}