I am trying to modify a website that was built by some other web developers.
The part in question, reads an XML data file and pulls back data to display on a Google Map.
They have a line of code;
string path = Server.MapPath(OutageXmlVirtualPath); //path to XML file
OutageData outages = XMLUtil.Deserialize<OutageData>(path);
Outage outage = outages.Outages.FirstOrDefault(o => o.PostCodes.Any(p => FoundOutagePostcode(p)) && !o.Planned);
That pulls the First record in the XML that matches a postcode the user has entered into a textbox. (lastOrDefault works also)
The issue with this however, is that the postcode they enter might appear more than once. In another node in the XML. So what I want to do is pull back all of the records in the XML that match. Not just the first. I can see that there is 'All' and 'SelectMany' methods, but dont know how to implement these into my code.
I would consider myself a complete novice in this area.
If anyone is able to lend any help that would be greatly appreciated.
Kind regards,
Chris
XML sample
<?xml version="1.0" encoding="utf-16"?>
<OutageData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<TimeStamp>2013-12-16T06:38:00.1706983+00:00</TimeStamp>
<Outages>
<Outage>
<Region>South West</Region>
<IncidentID>INCD-83651-m</IncidentID>
<ConfirmedOff>1</ConfirmedOff>
<PredictedOff>0</PredictedOff>
<Restored>0</Restored>
<Status>In Progress</Status>
<Planned>false</Planned>
<StartTime>2013-12-14T18:03:00</StartTime>
<ETR>2013-12-16T12:00:00</ETR>
<Voltage>LV</Voltage>
<PostCodes>
<string>PL1 4RL</string>
<string>PL2 1AF</string>
<string>PL2 1AG</string>
<string>PL2 1AH</string>
</PostCodes>
<Sensitive>1</Sensitive>
</Outage>
<Outage>
<Region>West Midlands</Region>
<IncidentID>INCD-12499-I</IncidentID>
<ConfirmedOff>0</ConfirmedOff>
<PredictedOff>0</PredictedOff>
<Restored>0</Restored>
<Status>In Progress</Status>
<Planned>true</Planned>
<StartTime>2013-12-13T10:00:00</StartTime>
<ETR xsi:nil="true" />
<Voltage>HV</Voltage>
<PostCodes>
<string>SY7 9AX</string>
<string>SY7 9AY</string>
<string>SY7 9AZ</string>
<string>SY7 9BE</string>
</PostCodes>
<Sensitive>0</Sensitive>
</Outage>
</Outages>
</OutageData>
just try to use Where
var outagesFound = outages.Outages.Where(o => o.PostCodes.Any(p => FoundOutagePostcode(p)) && !o.Planned);
and then you can iterate through the outagesfound
list using the foreach
loop
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.