LINQXML - 循环查询结果(返回的元素比预期的低)

问题描述 投票:0回答:1

我正在尝试循环浏览LINQXML查询的结果。当我使用.Element(1).Value时,我可以返回一个字符串结果,但现在我想返回所有的Elements(),并循环浏览它们。

我在循环中显示结果的值,它只显示元素 "Name "和它的值。 我希望值能包括 "字符 "的所有XML。

你可以在这里运行并查看结果。https:/dotnetfiddle.netg0nURp。

我很困惑,我是应该像下面这样做,还是1)IEnumerable results =,还是2)在Select语句中列出所有我想要的值(如果这样做,我还想知道你是如何在结果中循环的)。

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

public class Program
{
    public static void Main()
    {
           XElement xelGOT =
              new XElement("GameOfThrones",
                 new XElement("Characters",
                     new XElement("Character",
                         new XAttribute("Status", "deceased"),
                         new XElement("Name", "Daenerys Targaryen"),
                         new XElement("Actor", "Emilia Clarke")
                         ),
                     new XElement("Character",
                         new XAttribute("Status", "living"),
                         new XElement("Name", "Jon Snow"),
                         new XElement("Actor", "Kit Harrington")
                         ),
                     new XElement("Character",
                         new XAttribute("Status", "living"),
                         new XElement("Name", "Tyrion Lannister"),
                         new XElement("Actor", "Peter Dinklage")
                         )
                      )
                );

            // This one works (except for the "orderby") 
            string secondLivingCharacter =
                (from xchar in xelGOT.Elements("Characters").First().Elements("Character")
                 where (string)xchar.Attribute("Status").Value == "living"
                 //orderby xchar.Element("Name")
                 select xchar).ElementAt(1).Value;
            Console.WriteLine("secondLivingCharacter=" + secondLivingCharacter); 

            // Get all the living characters 
            IEnumerable results =   
                (from xchar in xelGOT.Elements("Characters").First().Elements("Character")
                where (string) xchar.Attribute("Status").Value == "living"
                select xchar).Elements();

            foreach (var result in results.Cast<XElement>())
            {
                Console.WriteLine("\n\nDebug result=" + result.ToString()); 
                Console.WriteLine(
                          " Character=" + result.Element("Name").Value +
                          " Actor=" + result.Element("Actor").Value +
                          " Status=" + result.Attribute("Status")
                     );
            }

    }
}

想要的结果是循环并打印出两个匹配字符的nameactor。

实际输出结果和错误。

secondLivingCharacter=Tyrion LannisterPeter Dinklage


    Debug result=<Name>Jon Snow</Name>
    Run-time exception (line 48): Object reference not set to an instance of an object.

    Stack Trace:

    [System.NullReferenceException: Object reference not set to an instance of an object.]
       at Program.Main() :line 48
c#-4.0 linq-to-xml
1个回答
1
投票

请尝试以下方法。

c#

void Main()
{
    XElement xelGOT =
              new XElement("GameOfThrones",
                 new XElement("Characters",
                     new XElement("Character",
                         new XAttribute("Status", "deceased"),
                         new XElement("Name", "Daenerys Targaryen"),
                         new XElement("Actor", "Emilia Clarke")
                         ),
                     new XElement("Character",
                         new XAttribute("Status", "living"),
                         new XElement("Name", "Jon Snow"),
                         new XElement("Actor", "Kit Harrington")
                         ),
                     new XElement("Character",
                         new XAttribute("Status", "living"),
                         new XElement("Name", "Tyrion Lannister"),
                         new XElement("Actor", "Peter Dinklage")
                         )
                      )
                );

    // This one works (except for the "orderby") 
    string secondLivingCharacter =
        (from xchar in xelGOT.Elements("Characters").First().Elements("Character")
         where xchar.Attribute("Status").Value.Equals("living")
         orderby xchar.Element("Name").value descending
         select xchar).ElementAt(1).Value;
    Console.WriteLine("secondLivingCharacter='{0}'{1}", secondLivingCharacter, Environment.NewLine);

    // Get all the living characters 
    var results =
        (from xchar in xelGOT.Descendants("Character")
         where (string)xchar.Attribute("Status").Value == "living"
         select xchar);

    foreach (var result in results)
    {
        Console.WriteLine("Character='{0}' Actor='{1}' Status='{2}'"
             , result.Element("Name").Value
             , result.Element("Actor").Value
             , result.Attribute("Status").Value);
    }

}
© www.soinside.com 2019 - 2024. All rights reserved.