繁体   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