我有一个大的xml文件包含许多testObject
个节点,但我必须只关注类类型LNCEL
和BTSSCL
的testobject。
此处LNCEL
和BTSSCL
与其distName
属性相关;例如,btsId = 12282
的第一个对象的父distName
就像PLMN-PLMN/MRBTS-12282
一样,只有这一部分必须与testobject
的{{1}}匹配,而LNCEL
的distName = {{ 1}}。
简而言之,我在这个XML文件中有很多对象,每个对象都需要名称属性PLMN-PLMN/MRBTS-12282
的值,这些值分散在两类btsId,btsName,mcc,mnc,name
和LNCEL
上。
xml的小片段如下所示:
BTSSCL
答案 0 :(得分:0)
请尝试以下操作:
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);
XElement root = doc.Root;
XNamespace ns = root.GetDefaultNamespace();
List<XElement> testObject = root.Descendants(ns + "testObject").Where(x => ((string)x.Attribute("class") == "BTSSCL") || ((string)x.Attribute("class") == "LNCEL")).ToList();
}
}
}