0

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>

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

var is of course an IEnumerable<Outage> which you can then do a foreach loop around ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.