2

I know this is probably a lot easier than I'm making it. I'm able to pull all the machines out of the XElement but I'm trying to figure out how to pull out the machines with a specific sequence number. In the below XML snippet, I'd like to use the machines where sequence = 1.

XML:

<Location>
  <Sequence>1</Sequence>
  <Machines>
    <Machine></Machine>
    <Machine></Machine>
  </Machines>
</Location>
<Location>
  <Sequence>2</Sequence>
  <Machines>
    <Machine></Machine>
    <Machine></Machine>
  </Machines>
</Location>

Code:

IEnumerable<XElement> locSeqMachines = 
                      from seq in LocationRows.Descendants("Location")
                      select seq;

var eMachines = locSeqMachines.Descendants("Machine");
foreach (var machine in eMachines)
{   
}
svick
  • 236,525
  • 50
  • 385
  • 514
Sparhawk
  • 137
  • 1
  • 1
  • 11

4 Answers4

2

Something like this should do the job:

int soughtId = 1; // Assuming this is coming from somewhere
string soughtIdStr = soughtId.ToString();
var machines = LocationRows.Descendants("Location")
                           .Where(l => (string)l.Element("Sequence") == 
                                       soughtIdStr)
                           .Descendants("Machine");
JLRishe
  • 99,490
  • 19
  • 131
  • 169
1

You can use XPath to select the nodes by a specific sequence:

XmlNodeList nodeList = root.SelectNodes("descendant::Location[Sequence='1']");
Alina B.
  • 1,256
  • 8
  • 18
1

This code will group all the Machine data out of a Location tag filtered on the Location's Sequence value:

var locSeqMachines = from seq in LocationRows.Descendants("Location")
                     where seq.Element("Sequence").Value == "1"
                     select new {
                         Sequence = seq.Element("Sequence").Value,
                         Machines = from m in seq.Descendants("Machines").Elements()
                                    select m.Value
                     };

Here's some code demonstrating how you can access the data (and test the snippet):

foreach (var location in locSeqMachines) {
    Console.WriteLine("sequence: {0}", location.Sequence);
    foreach (var machine in location.Machines) {
        Console.WriteLine(" machine: {0}", machine);
    }
}
Jay Riggs
  • 53,046
  • 9
  • 139
  • 151
0

In parsing the given xml, you can use this method to arrive with the answer without raising an error of multiple root elements.

    var xmlText =   @"<root>
                       <Location>
                        <Sequence>1</Sequence>
                        <Machines>
                          <Machine></Machine>
                          <Machine></Machine>
                        </Machines>
                       </Location>
                       <Location>
                        <Sequence>2</Sequence>
                        <Machines>
                          <Machine></Machine>
                          <Machine></Machine>
                         </Machines>
                       </Location>
                      </root>";

    var elements     = XElement.Parse(xmlText);
    var machineWith1 = from subElem in elements.Elements("Location")
                           where subElem.Element("Sequence").Value == "1"
                           select subElem.Element("Machines").Elements("Machine");

then you can check the value of machineWith1 for this,

Community
  • 1
  • 1
roybalderama
  • 1,650
  • 21
  • 38