Note that there are some explanatory texts on larger screens.

plurals
  1. POSwing and a miss LINQ to XML query
    primarykey
    data
    text
    <p>I have the following XML</p> <pre><code>&lt;AllVendors xmlns="http://important/data"&gt; &lt;Vendor&gt; &lt;VendorId&gt;1&lt;/VendorId&gt; &lt;VendorName&gt;Vendor A&lt;/VendorName&gt; &lt;/Vendor&gt; &lt;Vendor&gt; &lt;VendorId&gt;2&lt;/VendorId&gt; &lt;VendorName&gt;Vendor B&lt;/VendorName&gt; &lt;/Vendor&gt; &lt;Vendor&gt; &lt;VendorId&gt;3&lt;/VendorId&gt; &lt;VendorName&gt;Vendor C&lt;/VendorName&gt; &lt;/Vendor&gt; &lt;/AllVendors&gt; </code></pre> <p>and the following CLR type</p> <pre><code>class Vendor { public Vendor(string vendorId, string vendorName) { VendorId = vendorId; VendorName = vendorName; } public string VendorId { get; set; } public string VendorName { get; set; } } </code></pre> <p>and the following basic interaction</p> <pre><code>class VendorInteraction { private List&lt;Vendor&gt; 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 =&gt; new Vendor(vendor.Value, vendor.Value)).ToList();** if (Vendors == null) Vendors = new List&lt;Vendor&gt;(); else Vendors.Clear(); Vendors.AddRange(vendors); } } </code></pre> <p>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:</p> <pre><code>1 1 Vendor A Vendor A 2 2 Vendor B Vendor B 3 3 Vendor C Vendor C </code></pre> <p>I would like each Vendor instance in the List to look like this:</p> <pre><code>1 Vendor A 2 Vendor B 3 Vendor C </code></pre> <p>I am not very good with LINQ and your help is tremendously appreciated!</p> <p><strong>Update: Actual solution</strong></p> <pre><code>var vendors = rawData.Descendants(ns + "AllVendors").Elements() .Select(x =&gt; new Vendor(x.Element(ns + "VendorId").Value, x.Element(ns + "VendorName").Value)) .ToList(); </code></pre> <p>@AVD and @BrokenGlass got me very close but it seems that I needed to get the Elements collection.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload