Note that there are some explanatory texts on larger screens.

plurals
  1. POC# Linq to XML query only returning 1 result
    primarykey
    data
    text
    <p>I have the following method which queries an XML file. I need it to return the information on ALL files which have the "System" element matching the 'sys' variable which i pass into my method.</p> <p>It works OK, but only returns 1 result, when i know that there is more than 1 match in the XML file.</p> <p>It's as if my Linq query just goes trhough the XML file until it finds a result then stops, whereas i need it to get a collection of ALL matches.</p> <pre><code>public ListViewItem getDMcollection(string sys) { XDocument doc = XDocument.Load(Form1.CSDBpath + Form1.projectName + "\\Data.xml"); var dms = from dm in doc.Descendants("dataModule") where dm.Descendants("system").First().Value == sys select dm; foreach (var module in dms) { ListViewItem item = new ListViewItem(new string[] { module.Element("DMC").Value, module.Element("techName").Value, module.Element("infoName").Value, module.Element("status").Value, module.Element("currentUser").Value, module.Element("validator").Value, module.Element("notes").Value, //dm.Element("size").Value + " kb", //dm.Element("dateMod").Value }); return item; } return null; } </code></pre> <p>This is a sample of the XML file:</p> <pre><code>&lt;DMs&gt; &lt;dataModule&gt; &lt;DMC&gt;DMC-AJ-A-29-13-54-00ZZZ-254Z-B_001-00.XML&lt;/DMC&gt; &lt;techName&gt;Pressure switch&lt;/techName&gt; &lt;infoName&gt;Clean mechanically&lt;/infoName&gt; &lt;system&gt;72&lt;/system&gt; &lt;subsystem&gt;13&lt;/subsystem&gt; &lt;subsubsystem&gt;60&lt;/subsubsystem&gt; &lt;status&gt;Checked Out&lt;/status&gt; &lt;notes&gt;-&lt;/notes&gt; &lt;currentUser&gt;JakeMemery&lt;/currentUser&gt; &lt;validator&gt;-&lt;/validator&gt; &lt;dateMod&gt;-&lt;/dateMod&gt; &lt;size&gt;-&lt;/size&gt; &lt;/dataModule&gt; &lt;dataModule&gt; &lt;DMC&gt;DMC-AJ-A-30-15-62-00AAA-066A-D_001-00.XML&lt;/DMC&gt; &lt;techName&gt;Pressure switch&lt;/techName&gt; &lt;infoName&gt;Support equipment and tools data&lt;/infoName&gt; &lt;system&gt;29&lt;/system&gt; &lt;subsystem&gt;13&lt;/subsystem&gt; &lt;subsubsystem&gt;54&lt;/subsubsystem&gt; &lt;status&gt;Checked In&lt;/status&gt; &lt;notes&gt;-&lt;/notes&gt; &lt;currentUser&gt;-&lt;/currentUser&gt; &lt;validator&gt;-&lt;/validator&gt; &lt;dateMod&gt;-&lt;/dateMod&gt; &lt;size&gt;-&lt;/size&gt; &lt;/dataModule&gt; &lt;dataModule&gt; &lt;DMC&gt;DMC-AJ-A-45-60-12-00AAA-420A-B_001-00.XML&lt;/DMC&gt; &lt;techName&gt;Pressure switch&lt;/techName&gt; &lt;infoName&gt;General fault isolation procedure&lt;/infoName&gt; &lt;system&gt;29&lt;/system&gt; &lt;subsystem&gt;20&lt;/subsystem&gt; &lt;subsubsystem&gt;10&lt;/subsubsystem&gt; &lt;status&gt;Checked In&lt;/status&gt; &lt;notes&gt;-&lt;/notes&gt; &lt;currentUser&gt;-&lt;/currentUser&gt; &lt;validator&gt;-&lt;/validator&gt; &lt;dateMod&gt;-&lt;/dateMod&gt; &lt;size&gt;-&lt;/size&gt; &lt;/dataModule&gt; &lt;/DMs&gt; </code></pre> <p>So as an example, i might pass in the value of 29 to my method. And as you can see the XML file above contains two 'System' 29 matches, but my program only retuns 1 of them - the first one.</p> <p>The method that calls the above and passes in the 'sys' variable is this:</p> <pre><code>public ListViewItem splitSNS(string fullSNSpath) { string sns = new String(fullSNSpath.ToCharArray().Where(c =&gt; Char.IsDigit(c)).ToArray()); if (sns.Length.ToString() == "6") { string sys = sns.Substring(4, 2); string subsys = sns.Substring(2, 2); string unit = sns.Substring(0, 2); ListViewItem dms = getDMcollection(sys, subsys, unit); return dms; } else if (sns.Length.ToString() == "4") { string sys = sns.Substring(2, 2); string subsys = sns.Substring(0, 2); ListViewItem dms = getDMcollection(sys, subsys); return dms; } else if (sns.Length.ToString() == "2") { string sys = sns.Substring(0, 2); ListViewItem dms = getDMcollection(sys); return dms; } return null; } </code></pre> <p>and an extract of the method which calls the above is </p> <pre><code> ListViewItem dms = newFilter.splitSNS(fullSNSpath); if (dms != null) { // showfilteredList(dms); listView1.Items.Add(dms); showStatus(dms); } else { MessageBox.Show("There are no DMs to be displayed"); } </code></pre>
    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