简体   繁体   English

C#/XML:读取给定元素的索引的属性值

[英]C#/XML : reading attributes value of an index given element

I have the following non-alterable XML file :我有以下不可更改的 XML 文件:

<products>
    <product>
        <attributes>
            <att name="Name" value="TOTO" />
            <att name="Surname" value="Toto" />
            <att name="Age" value="10" />
        </attributes>
    </product>
    <product>
        <attributes>
            <att name="Name" value="TATA" />
            <att name="Surname" value="Tata" />
            <att name="Age" value="20" />
        </attributes>
    </product>
    <product>
        <attributes>
            <att name="Name" value="TITI" />
            <att name="Surname" value="Titi" />
            <att name="Age" value="30" />
        </attributes>
    </product>
</products>

Using C#, I need to extract the value of the value field for the nodes which value is equal to Name and Age , at a given index.使用 C#,我需要在给定的索引处提取值等于NameAge的节点的value字段的值。 Good example : inputing 2 would return a string containing TITI and another string containing 30 .很好的例子:输入2将返回一个包含TITI的字符串和另一个包含30 的字符串。

For the moment, I'm using an XmlDocument to load the XML file, and a XmlNodeList with the GetElementsByTagName method to get all the attributes ;目前,我使用 XmlDocument 加载 XML 文件,使用 XmlNodeList 使用 GetElementsByTagName 方法获取所有属性; then, I can display those elements, but I don't manage to display only two of them, depending of attributes names and an index.然后,我可以显示这些元素,但我不能只显示其中两个,这取决于属性名称和索引。

At best, I need a method like myXmlNodeList.Node["att"].Attributes["Name"].AtIndex(x) .充其量,我需要一个像myXmlNodeList.Node["att"].Attributes["Name"].AtIndex(x) 这样的方法

Could anyone help me ?有人可以帮我吗?

Try this尝试这个

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string xml =
                "<products>" +
                    "<product>" +
                        "<attributes>" +
                            "<att name=\"Name\" value=\"TOTO\" />" +
                            "<att name=\"Surname\" value=\"Toto\" />" +
                            "<att name=\"Age\" value=\"10\" />" +
                        "</attributes>" +
                    "</product>" +
                    "<product>" +
                        "<attributes>" +
                            "<att name=\"Name\" value=\"TATA\" />" +
                            "<att name=\"Surname\" value=\"Tata\" />" +
                            "<att name=\"Age\" value=\"20\" />" +
                        "</attributes>" +
                    "</product>" +
                    "<product>" +
                        "<attributes>" +
                            "<att name=\"Name\" value=\"TITI\" />" +
                            "<att name=\"Surname\" value=\"Titi\" />" +
                            "<att name=\"Age\" value=\"30\" />" +
                        "</attributes>" +
                    "</product>" + 
                "</products>";

            XElement products = XElement.Parse(xml);

            var results = products.Elements("product").Select(x => new
            {
                name = x.Descendants("att").Where(y => y.Attribute("name").Value == "Name").Select(z => z.Attribute("value").Value).FirstOrDefault(),
                age = x.Descendants("att").Where(y => y.Attribute("name").Value == "Age").Select(z => (int)z.Attribute("value")).FirstOrDefault()
            }).ToList();

            string name = results[2].name;
            int age = results[2].age;

        }
    }


}
​

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM