如何处理在相同级别上分散的相同xmlnodes的xml文件

时间:2018-01-07 16:17:13

标签: c# .net xml

我有一个大的xml文件包含许多testObject个节点,但我必须只关注类类型LNCELBTSSCL的testobject。

此处LNCELBTSSCL与其distName属性相关;例如,btsId = 12282的第一个对象的父distName就像PLMN-PLMN/MRBTS-12282一样,只有这一部分必须与testobject的{​​{1}}匹配,而LNCEL的distName = {{ 1}}。

简而言之,我在这个XML文件中有很多对象,每个对象都需要名称属性PLMN-PLMN/MRBTS-12282的值,这些值分散在两类btsId,btsName,mcc,mnc,nameLNCEL上。

xml的小片段如下所示:

BTSSCL

1 个答案:

答案 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();
        }
    }
}