C# 按属性值获取元素索引

C# 按属性值获取元素索引,c#,xml,xmlnodelist,C#,Xml,Xmlnodelist,情况:我有一个XML文件(大部分是布尔逻辑)。我想做的是: 通过节点中属性的内部文本获取节点的索引。然后将子节点添加到给定索引 例如: <if attribute="cat"> </if> <if attribute="dog"> </if> <if attribute="rabbit"> </if> 但是如何使用属性的innertext获取节点列表中节点的索引呢 基本上是按照 Somecode.IndexOf.Attri

情况:我有一个XML文件(大部分是布尔逻辑)。我想做的是: 通过节点中属性的内部文本获取节点的索引。然后将子节点添加到给定索引

例如:

<if attribute="cat">
</if>
<if attribute="dog">
</if>
<if attribute="rabbit">
</if>
但是如何使用属性的innertext获取节点列表中节点的索引呢

基本上是按照

Somecode.IndexOf.Attribute.Innertext("dog").Append(ChildNode);
以这种方式结束

<if attribute="cat">
</if>
<if attribute="dog">
    <if attribute="male">
    </if>
</if>
<if attribute="rabbit">
</if>


创建节点并将其插入索引,我没有问题。只需要一种获取索引的方法。

linq select函数有一个覆盖,它提供当前索引:

            string xml = @"<doc><if attribute=""cat"">
</if>
<if attribute=""dog"">
</if>
<if attribute=""rabbit"">
</if></doc>";

            XDocument d = XDocument.Parse(xml);

            var indexedElements = d.Descendants("if")
                    .Select((node, index) => new Tuple<int, XElement>(index, node)).ToArray()  // note: materialise here so that the index of the value we're searching for is relative to the other nodes
                    .Where(i => i.Item2.Attribute("attribute").Value == "dog");


            foreach (var e in indexedElements)
                Console.WriteLine(e.Item1 + ": " + e.Item2.ToString());

            Console.ReadLine();
stringxml=@”
";
XDocument d=XDocument.Parse(xml);
var indexedElements=d.Dimensions(“如果”)
.Select((node,index)=>new Tuple(index,node)).ToArray()//注意:在此处实现,以便我们正在搜索的值的索引相对于其他节点
其中(i=>i.Item2.Attribute(“Attribute”).Value==“dog”);
foreach(指数中的var e)
Console.WriteLine(e.Item1+“:“+e.Item2.ToString());
Console.ReadLine();

为了完整起见,这与Nathan的回答相同,只使用匿名类而不是元组:

using System;
using System.Linq;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main()
        {
            string xml = "<root><if attribute=\"cat\"></if><if attribute=\"dog\"></if><if attribute=\"rabbit\"></if></root>";
            XElement root = XElement.Parse(xml);

            int result = root.Descendants("if")
                .Select(((element, index) => new {Item = element, Index = index}))
                .Where(item => item.Item.Attribute("attribute").Value == "dog")
                .Select(item => item.Index)
                .First();

            Console.WriteLine(result);
        }
    }
}
使用系统;
使用System.Linq;
使用System.Xml.Linq;
命名空间控制台应用程序1
{
班级计划
{
静态void Main()
{
字符串xml=”“;
XElement root=XElement.Parse(xml);
int result=root.substands(“if”)
.Select(((元素,索引)=>new{Item=element,index=index}))
.Where(item=>item.item.Attribute(“Attribute”)。Value==“dog”)
.Select(item=>item.Index)
.First();
控制台写入线(结果);
}
}
}
using System;
using System.Linq;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main()
        {
            string xml = "<root><if attribute=\"cat\"></if><if attribute=\"dog\"></if><if attribute=\"rabbit\"></if></root>";
            XElement root = XElement.Parse(xml);

            int result = root.Descendants("if")
                .Select(((element, index) => new {Item = element, Index = index}))
                .Where(item => item.Item.Attribute("attribute").Value == "dog")
                .Select(item => item.Index)
                .First();

            Console.WriteLine(result);
        }
    }
}