Swing 和 LINQ to XML 查询的缺失

发布于 2024-12-21 09:57:57 字数 2016 浏览 0 评论 0原文

我有以下 XML

<AllVendors xmlns="http://important/data">
    <Vendor>
      <VendorId>1</VendorId> 
      <VendorName>Vendor A</VendorName> 
    </Vendor>
    <Vendor>
      <VendorId>2</VendorId> 
      <VendorName>Vendor B</VendorName> 
    </Vendor>
    <Vendor>
      <VendorId>3</VendorId> 
      <VendorName>Vendor C</VendorName> 
    </Vendor>
</AllVendors>

、以下 CLR 类型

class Vendor
{
    public Vendor(string vendorId, string vendorName)
    {
        VendorId = vendorId;
        VendorName = vendorName;
    }

    public string VendorId { get; set; }
    public string VendorName { get; set; }
}

和以下基本交互

class VendorInteraction
{
    private List<Vendor> Vendors { get; set; }

    private void GetVendors(string xml)
    {
        var rawData = XElement.Parse(xml);
        XNamespace ns = Constants.TheNamespace;

        **var vendors = rawData.Descendants(ns + "AllVendors").Elements().Elements().Select(vendor => new Vendor(vendor.Value, vendor.Value)).ToList();**

        if (Vendors == null)
            Vendors = new List<Vendor>();
        else
            Vendors.Clear();

        Vendors.AddRange(vendors);
    }
}

我的目标是返回一个基于 LINQ to XML 查询投影的列表。到目前为止,我的查询会产生如下输出:

1 1
Vendor A Vendor A
2 2 
Vendor B Vendor B
3 3
Vendor C Vendor C

我希望列表中的每个供应商实例如下所示:

1 Vendor A
2 Vendor B
3 Vendor C

我不太擅长 LINQ,非常感谢您的帮助!

更新:实际解决方案

var vendors = rawData.Descendants(ns + "AllVendors").Elements()
                     .Select(x => new Vendor(x.Element(ns + "VendorId").Value,
                                            x.Element(ns + "VendorName").Value))
                     .ToList();

@AVD 和 @BrokenGlass 让我非常接近,但似乎我需要获取 Elements 集合。

I have the following XML

<AllVendors xmlns="http://important/data">
    <Vendor>
      <VendorId>1</VendorId> 
      <VendorName>Vendor A</VendorName> 
    </Vendor>
    <Vendor>
      <VendorId>2</VendorId> 
      <VendorName>Vendor B</VendorName> 
    </Vendor>
    <Vendor>
      <VendorId>3</VendorId> 
      <VendorName>Vendor C</VendorName> 
    </Vendor>
</AllVendors>

and the following CLR type

class Vendor
{
    public Vendor(string vendorId, string vendorName)
    {
        VendorId = vendorId;
        VendorName = vendorName;
    }

    public string VendorId { get; set; }
    public string VendorName { get; set; }
}

and the following basic interaction

class VendorInteraction
{
    private List<Vendor> Vendors { get; set; }

    private void GetVendors(string xml)
    {
        var rawData = XElement.Parse(xml);
        XNamespace ns = Constants.TheNamespace;

        **var vendors = rawData.Descendants(ns + "AllVendors").Elements().Elements().Select(vendor => new Vendor(vendor.Value, vendor.Value)).ToList();**

        if (Vendors == null)
            Vendors = new List<Vendor>();
        else
            Vendors.Clear();

        Vendors.AddRange(vendors);
    }
}

My goal is to return a List that would be projected based on a LINQ to XML query. The query that I have so far produces output like this:

1 1
Vendor A Vendor A
2 2 
Vendor B Vendor B
3 3
Vendor C Vendor C

I would like each Vendor instance in the List to look like this:

1 Vendor A
2 Vendor B
3 Vendor C

I am not very good with LINQ and your help is tremendously appreciated!

Update: Actual solution

var vendors = rawData.Descendants(ns + "AllVendors").Elements()
                     .Select(x => new Vendor(x.Element(ns + "VendorId").Value,
                                            x.Element(ns + "VendorName").Value))
                     .ToList();

@AVD and @BrokenGlass got me very close but it seems that I needed to get the Elements collection.

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

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

发布评论

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

评论(2

猫七 2024-12-28 09:57:57

试试这个,

var vendors = rawData.Descendants(ns+"Vendor")
                    .Select(x => new Vendor(x.Element(ns+"VendorId").Value,
                                            x.Element(ns+"VendorName").Value))
                    .ToList();

Try this,

var vendors = rawData.Descendants(ns+"Vendor")
                    .Select(x => new Vendor(x.Element(ns+"VendorId").Value,
                                            x.Element(ns+"VendorName").Value))
                    .ToList();
羅雙樹 2024-12-28 09:57:57

您想要作为 Vendor 构造函数参数的值本身就是 Vendor 元素中的子元素:

var vendors = rawData.Descendants(ns + "Vendor")
                     .Select(x => new Vendor(x.Element("VendorId").Value,
                                             x.Element("VendorName").Value))
                     .ToList();

The values you want as constructor parameters for Vendor are themselves sub-elements within the Vendor element:

var vendors = rawData.Descendants(ns + "Vendor")
                     .Select(x => new Vendor(x.Element("VendorId").Value,
                                             x.Element("VendorName").Value))
                     .ToList();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文