Trying to extract list of “Queue” Elements into a new list of Queue objects. I have the following xml:
<MSMQData>
<Queues>
<Queue env="LOCAL" server="WORK150">FormatName:DIRECT=OS:WKSTN150\private$\localqueue1</Queue>
<Queue env="TEST" server="TEST01">FormatName:DIRECT=OS:dev-test01\private$\testqueue</Queue>
<Queue env="PROD" server="empty"></Queue>
<Queue env="PROD" server="empty"></Queue>
</Queues>
</MSMQData>
Here’s my code that obviously does not bring me back a list of Queue Elements that I’m trying to retrieve. What am I missing here?
var queues = (from col in xmlMSMQLoad.Descendants("Queues")
select col)
.Select(c => new Queue
{
Environment = c.Element("Queue").Attribute("env").Value,
Server = c.Element("Queue").Attribute("server").Value,
QueueName = c.Element("Queue").Value
})
.ToList();
By the way I do have a class called Queue with these properties in it.
Your current approach finds “Queues” then only selects the first “Queue” item. It doesn’t grab all of them. What you want to do is use
Descendants("Queue")or first select the “Queues” element and then its “Queue” elements.Use this query instead: