1

我有这个xml。如何<region>根据“部分”选择一个?

<PhoneAndAddresses>
  <PageTitle></PageTitle>
  <region section="CityServices">
    <Business>
      <Name>
        Atlanta Police Department
        <Name>
          <Address>612 Magnolia St NW, Atlanta, GA 30314</Address>
          <Phone>404-658-6486</Phone>
        </Business>
    <Business>
      <Name>Atlanta Police Department</Name>
      <Address>398 Centennial Olympic Park Dr NW, Atlanta, GA 30313</Address>
      <Phone>404-658-6636</Phone>
    </Business>
  </region>
  <region section="Hospitals">
    <Business>
      <Name>
        Emory University Hospital
        <Name>
          <Address>612 Magnolia St NW, Atlanta, GA 30314</Address>
          <Phone>404-658-6486</Phone>
        </Business>
    <Business>
      <Name>
        St Joseph's Hospital
        <Name>
          <Address>398 Centennial Olympic Park Dr NW, Atlanta, GA 30313</Address>
          <Phone>404-658-6636</Phone>
        </Business>
  </region>
</PhoneAndAddresses>
4

2 回答 2

2

采用:

var result = XDocument.Parse(input).Descendants("region")
    .FirstOrDefault(e => (string)e.Attribute("section") == "CityServices");

或使用 XPath:

//region[@section = 'CityServices']
于 2012-11-14T00:16:49.350 回答
2

尝试这样的事情(未经测试):

var doc = XDocument.Parse(xmltext);
var selectedRegion = doc.Root.Descendents("region").FirstOrDefault(r => r.Attribute("section").Value == "target value");
于 2012-11-14T00:17:53.057 回答