C# 遍历XML元素列表

C# 遍历XML元素列表,c#,linq-to-xml,C#,Linq To Xml,我想跟进之前提出的问题,但显然我不允许在同一个线程中继续这一问题 在我前面的问题()中,我在寻找xml中所有URL的值。但是,访问同一节点内不同值的正确方法是什么? 例如: <list index="1" name="" title="" id="5702f74a-9df2-12e5-89e0-f947f6dd0a1f"> <theme> <properties> <field index="1"/> <f

我想跟进之前提出的问题,但显然我不允许在同一个线程中继续这一问题

在我前面的问题()中,我在寻找xml中所有URL的值。但是,访问同一节点内不同值的正确方法是什么? 例如:

<list index="1" name="" title="" id="5702f74a-9df2-12e5-89e0-f947f6dd0a1f">
  <theme>
    <properties>
      <field index="1"/>
      <field index="2" name="title"></field>
    </properties>
    <list>
      <item id="f391fada-90c5-f239-a836-25ca76311286">
         <field index="1">
           <url>49e424a8ae1bf4707bf4d27c4614e905.png</url>
           <title></title>
         </field>
         <field index="2" name="question">This is a question</field>
         <field index="3" name="answer">This is an answer</field>
         <field index="4" name="remark"/>
         <field index="5" name="reveal"/>
       </item>

       <item id="a0b97163-d195-4dce-b970-ecb6eb080403">
         <field index="1">
            <url>49e424a8ae1bf4707bf4d27c4614e905.png</url>
            <title></title>
         </field>
         <field index="2" name="question"/>
         <field index="3" name="answer"></field>
         <field index="4" name="remark"/>
         <field index="5" name="reveal"/>
       </item>
     </list>
  </theme>
</list>
我是否必须对每个变量使用排序查询

var string = xmlDoc.Descendants("list")
    .Where(e => (int)e.Attribute("index") == 1)
    .Descendants("item").Descendants("field")
    .Where(e => (string)e.Attribute("index") == "1")
    .Select(e => (string)e.Value());
或者有没有一种方法可以获取列表中的字段,然后我可以通过它的indexnumber进行搜索

foreach (field in fields)
{
  switch case (field.name)
  {
    case "question":
      question = field.value;
      break;

    case "answer":
      answer= field.value;
      break;
  }
}

非常感谢您的任何想法。

我将创建一个助手方法,将字段解析为一个包含所有属性的
对象,并按如下方式使用它:

var items = from item in doc.Descendants("item")
            let fields = item.Elements("field")             
            select ItemFromFields(fields);
public class Item
{
    public string Url { get; set; }
    public string Question { get; set; }
    public string Answer { get; set; }
    public string Remark { get; set; }
    public string Reveal { get; set; }
}
helper方法将打开字段索引,您可以这样实现它:

private static Item ItemFromFields(IEnumerable<XElement> fields)
{
    var item = new Item();

    foreach (var field in fields)
    {
        var index = (int)field.Attribute("index");

        switch (index)
        {
            case 1:
                item.Url = (string)field.Element("url");
                break;
            case 2:
                item.Question = field.Value;
                break;
            case 3:
                item.Answer = field.Value;
                break;
            case 4:
                item.Remark = field.Value;
                break;
            case 5:
                item.Reveal = field.Value;
                break;
        }
    }

    return item;
}

试试这个。URL字段不包含名称或索引标记,因此必须测试名称标记是否不等于null

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

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            string input = 
                "<list index=\"1\" name=\"\" title=\"\" id=\"5702f74a-9df2-12e5-89e0-f947f6dd0a1f\">\n" +
                   "<theme>\n" +
                     "<properties>\n" +
                       "<field index=\"1\"/>\n" +
                       "<field index=\"2\" name=\"title\"></field>\n" +
                     "</properties>\n" +
                     "<list>\n" +
                       "<item id=\"f391fada-90c5-f239-a836-25ca76311286\">\n" +
                          "<field index=\"1\">\n" +
                            "<url>49e424a8ae1bf4707bf4d27c4614e905.png</url>\n" +
                            "<title></title>\n" +
                          "</field>\n" +
                          "<field index=\"2\" name=\"question\">This is a question</field>\n" +
                          "<field index=\"3\" name=\"answer\">This is an answer</field>\n" +
                          "<field index=\"4\" name=\"remark\"/>\n" +
                          "<field index=\"5\" name=\"reveal\"/>\n" +
                        "</item>\n" +

                        "<item id=\"a0b97163-d195-4dce-b970-ecb6eb080403\">\n" +
                          "<field index=\"1\">\n" +
                             "<url>49e424a8ae1bf4707bf4d27c4614e905.png</url>\n" +
                             "<title></title>\n" +
                          "</field>\n" +
                          "<field index=\"2\" name=\"question\"/>\n" +
                          "<field index=\"3\" name=\"answer\"></field>\n" +
                          "<field index=\"4\" name=\"remark\"/>\n" +
                          "<field index=\"5\" name=\"reveal\"/>\n" +
                        "</item>\n" +
                      "</list>\n" +
                   "</theme>\n" +
                 "</list>\n";

            XDocument doc = XDocument.Parse(input);
            var results = doc.Descendants("item")
                .Select(w => new {
                    url = w.Descendants("url").FirstOrDefault().Value,
                    question = w.Descendants("field").Where(x => x.Attribute("name") != null).Where(y => y.Attribute("name").Value == "question").Select(z => z.Attribute("index").Value).FirstOrDefault(),
                    answer = w.Descendants("field").Where(x => x.Attribute("name") != null).Where(y => y.Attribute("name").Value == "answer").Select(z => z.Attribute("index").Value).FirstOrDefault(),
                    remark = w.Descendants("field").Where(x => x.Attribute("name") != null).Where(y => y.Attribute("name").Value == "remark").Select(z => z.Attribute("index").Value).FirstOrDefault(),
                    reveal = w.Descendants("field").Where(x => x.Attribute("name") != null).Where(y => y.Attribute("name").Value == "reveal").Select(z => z.Attribute("index").Value).FirstOrDefault()
                })
                .ToList();
        }
    }
}
​
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Xml;
使用System.Xml.Linq;
命名空间控制台应用程序2
{
班级计划
{
静态void Main(字符串[]参数)
{
字符串输入=
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“49e424a8ae1bf4707bf4d27c4614e905.png\n”+
“\n”+
“\n”+
“这是一个问题\n”+
“这是一个答案\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“49e424a8ae1bf4707bf4d27c4614e905.png\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“\n”;
XDocument doc=XDocument.Parse(输入);
风险值结果=单据子体(“项目”)
.选择(w=>new{
url=w.subjects(“url”).FirstOrDefault()值,
question=w.Attribute(“字段”)。其中(x=>x.Attribute(“name”)!=null)。其中(y=>y.Attribute(“name”)。Value==“question”)。选择(z=>z.Attribute(“index”).Value)。FirstOrDefault(),
answer=w.attributes(“field”)。其中(x=>x.Attribute(“name”)!=null)。其中(y=>y.Attribute(“name”)。Value==“answer”)。选择(z=>z.Attribute(“index”).Value)。FirstOrDefault(),
备注=w.attributes(“field”)。其中(x=>x.Attribute(“name”)!=null)。其中(y=>y.Attribute(“name”)。Value==“备注”)。选择(z=>z.Attribute(“index”).Value)。FirstOrDefault(),
显示=w.field(“字段”)。其中(x=>x.Attribute(“name”)!=null)。其中(y=>y.Attribute(“name”)。Value==“显示”)。选择(z=>z.Attribute(“index”).Value)。FirstOrDefault()
})
.ToList();
}
}
}
​

其中(x=>x.Attribute(“name”)!=null)。其中(y=>y.Attribute(“name”).Value==“answer”)
可以简化为
其中(x=>(string)x.Attribute(“name”)==“answer”)
。它可以。你试过了吗?这两者是等效的。是的,但它不起作用,因为URL标记不包含名称属性,并给出了一个异常。阅读我在代码中的注释。我只能假设您没有尝试过。
x.Attribute(“name”)
当没有name属性时,将返回
null
。将其转换为
字符串
也会返回
null
,这可以与
“answer”
或任何其他值进行比较。也不例外,嗯?检查答案是否为
答案
-没有空检查。如果你愿意,我会把它们全部换掉-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            string input = 
                "<list index=\"1\" name=\"\" title=\"\" id=\"5702f74a-9df2-12e5-89e0-f947f6dd0a1f\">\n" +
                   "<theme>\n" +
                     "<properties>\n" +
                       "<field index=\"1\"/>\n" +
                       "<field index=\"2\" name=\"title\"></field>\n" +
                     "</properties>\n" +
                     "<list>\n" +
                       "<item id=\"f391fada-90c5-f239-a836-25ca76311286\">\n" +
                          "<field index=\"1\">\n" +
                            "<url>49e424a8ae1bf4707bf4d27c4614e905.png</url>\n" +
                            "<title></title>\n" +
                          "</field>\n" +
                          "<field index=\"2\" name=\"question\">This is a question</field>\n" +
                          "<field index=\"3\" name=\"answer\">This is an answer</field>\n" +
                          "<field index=\"4\" name=\"remark\"/>\n" +
                          "<field index=\"5\" name=\"reveal\"/>\n" +
                        "</item>\n" +

                        "<item id=\"a0b97163-d195-4dce-b970-ecb6eb080403\">\n" +
                          "<field index=\"1\">\n" +
                             "<url>49e424a8ae1bf4707bf4d27c4614e905.png</url>\n" +
                             "<title></title>\n" +
                          "</field>\n" +
                          "<field index=\"2\" name=\"question\"/>\n" +
                          "<field index=\"3\" name=\"answer\"></field>\n" +
                          "<field index=\"4\" name=\"remark\"/>\n" +
                          "<field index=\"5\" name=\"reveal\"/>\n" +
                        "</item>\n" +
                      "</list>\n" +
                   "</theme>\n" +
                 "</list>\n";

            XDocument doc = XDocument.Parse(input);
            var results = doc.Descendants("item")
                .Select(w => new {
                    url = w.Descendants("url").FirstOrDefault().Value,
                    question = w.Descendants("field").Where(x => x.Attribute("name") != null).Where(y => y.Attribute("name").Value == "question").Select(z => z.Attribute("index").Value).FirstOrDefault(),
                    answer = w.Descendants("field").Where(x => x.Attribute("name") != null).Where(y => y.Attribute("name").Value == "answer").Select(z => z.Attribute("index").Value).FirstOrDefault(),
                    remark = w.Descendants("field").Where(x => x.Attribute("name") != null).Where(y => y.Attribute("name").Value == "remark").Select(z => z.Attribute("index").Value).FirstOrDefault(),
                    reveal = w.Descendants("field").Where(x => x.Attribute("name") != null).Where(y => y.Attribute("name").Value == "reveal").Select(z => z.Attribute("index").Value).FirstOrDefault()
                })
                .ToList();
        }
    }
}
​