简体   繁体   English

C#XML选择多个节点

[英]C# XML Select Multiple Nodes

I am trying to modify a website that was built by some other web developers. 我正在尝试修改由其他一些Web开发人员构建的网站。

The part in question, reads an XML data file and pulls back data to display on a Google Map. 有问题的部分读取XML数据文件,然后拉回数据以显示在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. 这将在XML中提取与用户已输入到文本框中的邮政编码匹配的First记录。 (lastOrDefault works also) (lastOrDefault也可以)

The issue with this however, is that the postcode they enter might appear more than once. 但是,这样做的问题是,他们输入的邮政编码可能会出现多次。 In another node in the XML. 在XML的另一个节点中。 So what I want to do is pull back all of the records in the XML that match. 因此,我要做的是撤回XML中所有匹配的记录。 Not just the first. 不只是第一个。 I can see that there is 'All' and 'SelectMany' methods, but dont know how to implement these into my code. 我可以看到有“全部”和“ SelectMany”方法,但是不知道如何在我的代码中实现这些方法。

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样本

<?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 只是尝试使用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 然后您可以使用foreach循环遍历outagesfound列表

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM