LinqToXML 为什么这不起作用?

发布于 11-02 10:54 字数 1921 浏览 4 评论 0原文

我有我的示例 XML 文件:

 <?xml version="1.0" encoding="utf-8" ?>
 <contacts>
   <contact contactId="2">
     <firstName>Barney</firstName>
     <lastName>Gottshall</lastName>
   </contact>
   <contact contactId="3">
     <firstName>Armando</firstName>
     <lastName>Valdes</lastName>
   </contact>
   <contact contactId="4">
     <firstName>Adam</firstName>
     <lastName>Gauwain</lastName>
   </contact>
 </contacts>

和我的程序:

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

public class Program
{
    public class contact
    {
        public int contactId { get; set; }
        public string firstName { get; set; }
        public string lastName { get; set; }
        public override string ToString()
        {
            return firstName+" "+lastName;
        }
    }

    public static void Main()
    {
        string test;
        XDocument xDocument = XDocument.Load("items.xml");

        var all = from a in xDocument.Elements("contact")
                  where a.Attribute("contactId")!=null 
                  select new contact
                             {
                                 contactId = (int) a.Attribute("contactId"),
                                 firstName = (string) a.Attribute("firstName"),
                                 lastName = (string) a.Attribute("lastName")

                             };

        if (all == null)
            Console.WriteLine("Null !");
        else
            foreach (contact t in all)
            {
                test = t.ToString();
                Console.WriteLine(t.ToString());
            }
        }
        Console.ReadKey();
    }
}

此代码向我显示空白控制台窗口。不存在“空”!并且没有接触元件。我花了很多时间思考为什么......有人可以帮助我吗?当我在 foreach 语句中放置断点时,它不起作用

I have my sample XML file:

 <?xml version="1.0" encoding="utf-8" ?>
 <contacts>
   <contact contactId="2">
     <firstName>Barney</firstName>
     <lastName>Gottshall</lastName>
   </contact>
   <contact contactId="3">
     <firstName>Armando</firstName>
     <lastName>Valdes</lastName>
   </contact>
   <contact contactId="4">
     <firstName>Adam</firstName>
     <lastName>Gauwain</lastName>
   </contact>
 </contacts>

and my Program:

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

public class Program
{
    public class contact
    {
        public int contactId { get; set; }
        public string firstName { get; set; }
        public string lastName { get; set; }
        public override string ToString()
        {
            return firstName+" "+lastName;
        }
    }

    public static void Main()
    {
        string test;
        XDocument xDocument = XDocument.Load("items.xml");

        var all = from a in xDocument.Elements("contact")
                  where a.Attribute("contactId")!=null 
                  select new contact
                             {
                                 contactId = (int) a.Attribute("contactId"),
                                 firstName = (string) a.Attribute("firstName"),
                                 lastName = (string) a.Attribute("lastName")

                             };

        if (all == null)
            Console.WriteLine("Null !");
        else
            foreach (contact t in all)
            {
                test = t.ToString();
                Console.WriteLine(t.ToString());
            }
        }
        Console.ReadKey();
    }
}

This code shows me blank console window. There's no "Null !" and no contact element. I spent a lot of time thinking why is that... could someone help me? when I put breakpoint inside foreach statement it's not working

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

美胚控场2024-11-09 10:54:40

实际上有几个原因。

  1. Elements 仅提供文档的顶级元素(“contacts”元素),因此您应该使用后代 来获取较低级别的元素。
  2. FirstName 和 LastName 是元素,而不是属性。

这是工作代码:

    var all = from a in xDocument.Descendants("contact")
              where a.Attribute("contactId")!=null 
              select new contact
                         {
                             contactId = (int) a.Attribute("contactId"),
                             firstName = (string) a.Element("firstName").Value,
                             lastName = (string) a.Element("lastName").Value
                         };

There are actually a couple of reasons.

  1. Elements only gives you top-level elements of the document (the "contacts" element), so you should use Descendants to get lower-level elements).
  2. firstName and lastName are elements, not attributes.

Here's working code:

    var all = from a in xDocument.Descendants("contact")
              where a.Attribute("contactId")!=null 
              select new contact
                         {
                             contactId = (int) a.Attribute("contactId"),
                             firstName = (string) a.Element("firstName").Value,
                             lastName = (string) a.Element("lastName").Value
                         };
乞讨2024-11-09 10:54:40

尝试使用 Descendants() 而不是 Elements()

var all = from a in xDocument.Descendants("contact")
                  where a.Attribute("contactId") != null 
                  select new contact
                             {
                                 contactId = (int) a.Attribute("contactId"),
                                 firstName = (string) a.Attribute("firstName"),
                                 lastName = (string) a.Attribute("lastName")

                             };

Element("contacts").Elements("contact")

var all = from a in xDocument.Element("contacts").Elements("contact")
                  where a.Attribute("contactId") != null 
                  select new contact
                             {
                                 contactId = (int) a.Attribute("contactId"),
                                 firstName = (string) a.Attribute("firstName"),
                                 lastName = (string) a.Attribute("lastName")

                             };

Try Descendants() instead of Elements()

var all = from a in xDocument.Descendants("contact")
                  where a.Attribute("contactId") != null 
                  select new contact
                             {
                                 contactId = (int) a.Attribute("contactId"),
                                 firstName = (string) a.Attribute("firstName"),
                                 lastName = (string) a.Attribute("lastName")

                             };

or Element("contacts").Elements("contact")

var all = from a in xDocument.Element("contacts").Elements("contact")
                  where a.Attribute("contactId") != null 
                  select new contact
                             {
                                 contactId = (int) a.Attribute("contactId"),
                                 firstName = (string) a.Attribute("firstName"),
                                 lastName = (string) a.Attribute("lastName")

                             };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文