xml 中使用 LINQ 的特定属性值列表
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>
无法打印特定属性的属性值列表,例如 ("ruleAttrib")
预期输出列表:-
- rule1Attribute
- rule2Attribute
- [空 Space]
- rule4Attribute
上面的索引 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();
Works Fine... as per the expected output.
尝试使用 xml linq 和正则表达式:
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}");
<?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>
无法打印特定属性的属性值列表,例如 ("ruleAttrib")
预期输出列表:-
- rule1Attribute
- rule2Attribute
- [空 Space]
- rule4Attribute
上面的索引 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();
Works Fine... as per the expected output.
尝试使用 xml linq 和正则表达式:
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}");