Swing 和 LINQ to XML 查询的缺失
我有以下 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个,
Try this,
您想要作为
Vendor
构造函数参数的值本身就是 Vendor 元素中的子元素:The values you want as constructor parameters for
Vendor
are themselves sub-elements within the Vendor element: