user46000
user46000

Reputation: 77

LINQ to xml: result based on attributes

I have an XML similar to this

<?xml version="1.0" encoding="UTF-8"?>
<schedule>

<Aircraft mdy="Thursday, September 1, 2016" Destination="London"  
  Source="Greece" ID="B747">
 <FROM>GRE</FROM>
 <TO>LON</TO>
 <Miles>3000</Miles>
 </Aircraft>

 <Aircraft mdy="Thursday, September 1, 2016" Destination="New York"  
  Source="Greece" ID="B747">
  <FROM>GRE</FROM>
  <TO>IT</TO>
  <Miles>20000</Miles>
  </Aircraft>

 <Aircraft mdy="Thursday, September 1, 2016" Destination="Mykonos"  
  Source="Athens" ID="A320">
  <FROM>ATH</FROM>
  <TO>MYK</TO>
  <Miles>300</Miles>
  </Aircraft>

  </schedule>

Please help me on the below query. My LINQ experience is limited and at a novice level.

var p = from a in reservation.Descendants("Aircrafts")
        where a.Attribute("ID").Value.Equals("B747")
        ...
        change array contents

I want to dynamically change the content of the array below based on ID attribute. For instance if ID=="B747" seats[100], else if ID=="A320" seats[200] etc.

static int p= 100;
public static bool[] seats;
seats = new bool[p];

Thanks in advance

Upvotes: 2

Views: 83

Answers (1)

Gilad Green
Gilad Green

Reputation: 37299

To just set p you can do:

var p = (from element in XDocument.Load("data.xml").Descendants("Aircraft")
         let type = element.Attribute("ID").Value
         where type.Equals("B747")
         select type == "B747" ? 100 :
                type == "A320" ? 200 : 0).FirstOrDefault();

You can also do something like this: (and then load it at the beginning and use it each time)

var seatsForModel = (from element in reservation.Descendants("Aircraft")
                     let type = element.Attribute("ID").Value
                     select new
                     {
                        AircraftModel = type,
                        Seats = type == "B747" ? 100 :
                                type == "A320" ? 200 : 0
                     }).Distinct().ToDictionary(key => key.AircraftModel, value => value.Seats);

// Output: [B747, 100],
//         [A320, 200]

bool[] seatsTaken = new bool[seatsForModel["B747"]];

I create a Dictionary<string,int> of aircraft model to the number of seats it has and then you can use it to create your bool[].

Though... I a better design would be to have somewhere a mapping between the model and the number of seats and then to join it to the items from the xml


As for not being able to access p in the other function wrap the linq in a function and call it from the other function:

public int GetNumberOfSeats(string aircraftModel)
{
    var p = (from element in XDocument.Load("data.xml").Descendants("Aircraft")
             let type = element.Attribute("ID").Value
             where type.Equals(aircraftModel)
             select type == "B747" ? 100 :
                    type == "A320" ? 200 : 0).FirstOrDefault();
    return p;
}

public void assignFirstClass()
{
    var p = GetNumberOfSeats("B747");
    bool[] seats = new bool[p];

    //Rest of code
}

Upvotes: 1

Related Questions