簡體   English   中英

使用 LINQ 的 xml 中特定屬性值的列表

[英]List of specific attribute's value in xml using LINQ

    <?xml version="1.0"?>
    <Rules>
        <Rule id="001">
            <Rule1> name="name1" ruleAttrib="rule1Attribute"</Rule1>
            <Rule2> name="name2" </Rule2>
        </Rule>

        <Rule id="002">
            <Rule3>name="name3"</Rule3>
            <Rule4>name="name4"</Rule4>
            <Rule5>name="name5" ruleAttrib="rule2Attribute"</Rule5>
        </Rule>

        <Rule id="003">
            <Rule6>name="name6"</Rule6>
            <Rule7>name="name7" ruleAttrib=""</Rule7>
        </Rule>

        <Rule id="004">
            <Rule8>name="name8"</Rule8>
            <Rule9>name="name9" </Rule9>
        </Rule>

        <Rule id="005" />

        <Rule id="006">
            <Rule10>name="name10"</Rule10>
            <Rule11>name="name11" ruleAttrib="rule4Attribute"</Rule11>
            <Rule12>name="name12"</Rule12>
        </Rule>
    </Rules>

無法為 eg ("ruleAttrib") 打印特定屬性的屬性值列表

預期輸出列表:-

  1. 規則 1 屬性
  2. 規則2屬性
  3. [空的空間]
  4. 規則4屬性

上面的索引 3 是空的,因為具有 id = 003 的規則包含不包含任何內容的 ruleAttrib。

嘗試過的代碼:-

XDocument xdoc = new XDocument.Load(xmlPath);
List<XElement> ruleGroupList = xdoc.Descendants("Rule").ToList();

foreach (var item in ruleGroupList)
        {
            if (item.Descendants().Attributes("ruleAttrib").exist)
            {
                List<XAttribute> ruleAttriblist = item.Descendants().Attributes("ruleAttrib").ToList();
                Console.WriteLine(ruleAttriblist );
            } 
        }  
        Console.ReadLine();
        foreach (var item in ruleGroupList)
        {

            if (item.Descendants().Attributes("ruleAttrib").Count() != 0)
            {
                List<XAttribute> ruleAttriblist  = item.Descendants().Attributes("ruleAttrib").ToList();
                
                foreach (var attrib in ruleAttriblist)
                {
                    Console.WriteLine(attrib.Value);
                }
            }

        }
        Console.ReadLine();

工作正常......按照預期的輸出。

嘗試以下使用 xml linq 和 regex :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Text.RegularExpressions;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);
            var results = doc.Descendants()
                .Where(x => (x.NodeType == XmlNodeType.Element) && (x.Elements().Count() == 0))
                .Select(x => GetAttributes((string)x))
                .Where(x => x != null)
                .Select(x => new { name = x.Groups["name"].Value, rule = x.Groups["rule"].Value })
                .ToList();
        }
        static Match GetAttributes(string innertext)
        {
            string pattern = "name=\"(?'name'[^\"]+)\"\\s+ruleAttrib=\"(?'rule'[^\"]*)";
            Match match = Regex.Match(innertext, pattern);
            if (!match.Success) match = null;

            return match;
        }
    }
}

ruleAttribute 不是一個屬性,但它實際上是元素的值(內部文本)。

您可以使用Linq來解析Element (Rulexx),然后使用正則Regex來解析與ruleAttrib關聯的值。

List<XElement> ruleGroupList = xdoc.Root.Descendants("Rule").ToList();
var regex = new Regex("ruleAttrib='(?<value>\\S*)'");
var result = ruleGroupList.Elements()
                          .Select(x=>x.Value)
                          .Where(x=> regex.IsMatch(x))
                          .Select(x=>regex.Match(x).Groups["value"].Value);

輸出

在此處輸入圖片說明

如果你想使用格式“1.rule1Attribute”,那么你可以使用

var result = ruleGroupList.Elements()
                          .Select(x=>x.Value)
                          .Where(x=> regex.IsMatch(x))
                          .Select((x,index)=>$"{index+1}.{regex.Match(x).Groups["value"].Value}");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM