使用 linq to xml 查询 xml 文件?

发布于 2024-12-22 15:12:01 字数 697 浏览 2 评论 0原文

我有以下 xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<Cus>
  <Customer Location="NJ">
    <Male Value="True" />
    <Name Value="xxx" />
   </Customer>
  <Customer Location="NY">
    <Male Value="True" />
    <Name Value="yyy" />
   </Customer>
</Cus>

我正在尝试使用 linq to xml 进行查询,以便根据客户位置获取male 的值。

这是查询:

  var Male = from e in doc.Descendants("Male")
             select new
             {
                 Male = e.Attribute("Value").Value.ToString()
             };

我能够获取男性的值,但我很困惑如何根据 xml 文件中客户的位置获取名称。如何在此处添加一个 where 条件来确定男性的位置客户。如果有人可以指导我,我将不胜感激。

I have the following xml file:

<?xml version="1.0" encoding="utf-8"?>
<Cus>
  <Customer Location="NJ">
    <Male Value="True" />
    <Name Value="xxx" />
   </Customer>
  <Customer Location="NY">
    <Male Value="True" />
    <Name Value="yyy" />
   </Customer>
</Cus>

I am trying to query using the linq to xml inorder to get the value of male based on the customer location.

Here is the query:

  var Male = from e in doc.Descendants("Male")
             select new
             {
                 Male = e.Attribute("Value").Value.ToString()
             };

I am able to get the value of the male but i am confused how to get the name based on the location of the customer in the xml file.How to add a where condition in here which determines the location of the customer.I would appreciate if some one can guide me.

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

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

发布评论

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

评论(3

栖迟 2024-12-29 15:12:01

根据您的问题 - 要根据位置选择 name 的值,您可以使用以下内容:

private string CountOfMales(XDocument doc, string locationToFilter)
{
  var selection = from customer in doc.Descendants("Customer")
                 .Where(c => c.Attribute("Location").Value == locationToFilter)
                 select new
                 {
                    MaleValue = customer.Element("Name").Attribute("Value").Value
                 };

                 return selection.FirstOrDefault().MaleValue;
}

Per your question - to select the value of name based on location, you could use something like:

private string CountOfMales(XDocument doc, string locationToFilter)
{
  var selection = from customer in doc.Descendants("Customer")
                 .Where(c => c.Attribute("Location").Value == locationToFilter)
                 select new
                 {
                    MaleValue = customer.Element("Name").Attribute("Value").Value
                 };

                 return selection.FirstOrDefault().MaleValue;
}
時窥 2024-12-29 15:12:01

在获取男性之前,您需要对 Customer 元素执行 where 子句。所以像这样:

var males = from customer in doc.Descendants("Customer")
            where "NY".Equals(customer.Attribute("Location").Value)
            select customer.Descendants("Male");

注意:这尚未经过测试,但它应该为您提供一些如何继续的指示。查看这篇关于 where 关键字的 MSDN 文章了解更多信息。

另外,如果有帮助的话,我总是更喜欢使用 LINQ 扩展 用于可枚举集合。我发现它们比子句关键字更容易阅读和编写。

You want to do the where clause on the Customer elements before getting the males. So something like this:

var males = from customer in doc.Descendants("Customer")
            where "NY".Equals(customer.Attribute("Location").Value)
            select customer.Descendants("Male");

Note: This has not been tested, but it should give you some indication of how to proceed. Check this MSDN article on the where keyword for more information.

Also, if it helps, I always prefer using the LINQ Extensions for enumerable collections. I find them much easier to read and write than the clause keywords.

内心荒芜 2024-12-29 15:12:01

对于类似的事情,我真的很喜欢 XML 扩展方法 SafeElement 和 SafeAttribute,因为它们使您可以查询 XML,而不必担心如果 XML 不包含您指定的元素或属性,就会遇到空值。

这些扩展方法的代码位于此处:

    public static XElement SafeElement(this XContainer container, string name)
    {
        return container.Element(name) ?? new XElement(name);
    }

    public static XAttribute SafeAttribute(this XElement element, string name)
    {
        return element.Attribute(name) ?? new XAttribute(name, "");
    }

您可以像这样使用它:

        var customers = xdoc.Descendants("Customer")
                        .Where(x => x.SafeAttribute("Location").Value == "NJ")
                        .Select(x => x.SafeElement("Male").SafeAttribute("Value").Value);

如果由于某种原因不存在 Location 属性或 Male 元素,您最终会得到一个空结果集而不是异常。

For something like this I really like the XML extension methods SafeElement and SafeAttribute as they let you query the XML without worrying about running into null values if the XML doesn't contain the elements or attributes you specified.

The code for these extension methods is here:

    public static XElement SafeElement(this XContainer container, string name)
    {
        return container.Element(name) ?? new XElement(name);
    }

    public static XAttribute SafeAttribute(this XElement element, string name)
    {
        return element.Attribute(name) ?? new XAttribute(name, "");
    }

You use it like this:

        var customers = xdoc.Descendants("Customer")
                        .Where(x => x.SafeAttribute("Location").Value == "NJ")
                        .Select(x => x.SafeElement("Male").SafeAttribute("Value").Value);

If for whatever reason the Location attribute or the Male element isn't present you end up with an empty result set instead of exceptions.

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