2016-01-22 90 views
1

覺得這是我的XML ..如何獲得XML的父元素的所有子元素在C#

<ListOfDestinations> 
    <Destination> 
    <City>Ahmedabad</City> 
    <Title>Cheap Flights to Ahmedabad</Title> 
    <Content> 
    <Top10PlacestoVisit> 
     <subTitle>1</subTitle> 
     <details>d1</details> 
     <subTitle>2</subTitle> 
     <details>d2</details> 
     <subTitle>3</subTitle> 
     <details>d3</details> 
     <subTitle>4</subTitle> 
     <details>d4</details> 
     <subTitle>5</subTitle> 
     <details>d5</details> 
     <subTitle>6</subTitle> 
     <details>d6</details> 
     <subTitle>7</subTitle> 
     <details>d7</details> 
     <subTitle>8</subTitle> 
     <details>d8</details> 
     <subTitle>9</subTitle> 
     <details>d9</details> 
     <subTitle>10</subTitle> 
     <details>d10</details> 
    </Top10PlacestoVisit> 
    </Content> 
    </Destination> 
</ListOfDestinations> 

,所以我希望得到所有subTitledetails到list.how我能做到這一點。這是我試過的。

XmlDocument DestinationXml = new XmlDocument(); 
DestinationXml.Load(Server.MapPath("~/Xml_Data/Destination.xml")); 
var xdoc = XDocument.Parse(DestinationXml.InnerXml); 


var selectedCity = xdoc.Descendants("ListOfDestinations") 
         .Select(d => d.Elements("Destination") 
         .Where(w => w.Element("City").Value == DesName)); 

在這個地方得到<Top10PlacestoVisit></Top10PlacestoVisit>

裏面的所有數據(字幕和細節)然後我創建了一個Model和賦值像this.but我不能讓subTitledetails一次到list.this是我在這裏獲得成功,但subTitle我怎樣才能得到試圖

foreach (var itemz in selectedCity) 
{ 
    var needed = itemz.Elements("Top10PlacestoVisit"); 
    foreach (var nitems in needed) 
    { 
     var getSubs = needed.Elements("details"); 
     foreach (var itm in getSubs) 
     { 
      details.Add(new Destinations 
      { 
       details = itm.Value 
     }); 
     } 
    } 
} 

ViewBag.details = details; 

210並在同一時間分配它。幫助我這一點。

回答

1

如果你堅持你的XML,那麼你可以通過使用這樣的假設結構做父Place元素,那麼它是一個小更強大和明顯:

var topPlacesForCity = doc.Descendants("Destination") 
    .Where(x => (string) x.Element("City") == "Ahmedabad") 
    .Descendants("Top10PlacestoVisit") 
    .Elements("Place") 
    .Select(place => new 
    { 
     SubTitle = (string) place.Element("subTitle"), 
     Details = (string) place.Element("details") 
    }); 

順便說一句,沒有理由將XML加載到XmlDocument到那麼就解析它直入XDocument,並把它扔掉。改爲使用XDocument.Load(Server.MapPath("..."))

+0

nice work.its working.thank you very – bill

1

你的xml看起來不太好結構。從我的角度來看,這是合理的把subTitledetails與之相關的一些成封閉的父元素是這樣的:

<Top10PlacestoVisit> 
    <Place> 
     <subTitle>1</subTitle> 
     <details>d1</details> 
    </Place> 

無論如何,如果你正在捆綁與結構您已經發布並且可以」 T改變它 - 你仍然能夠實現你的目標是這樣的:在這裏

var details = xdoc.XPathSelectElements(
     string.Format("/ListOfDestinations/Destination[City='{0}']/Content/Top10PlacestoVisit/details", DesName)) 
    .Select(d => new { subTitle = (d.PreviousNode as XElement).Value, details = d.Value}) 
    .ToList(); 

注意以這種方式使用的XPathSelectElements得到的details收藏是我個人的喜好,你可以做任何你喜歡的。這裏的主要想法是:

.Select(d => new { subTitle = (d.PreviousNode as XElement).Value, details = d.Value}) 

關於收集details元素。因此,您只需使用PreviousNode屬性XElement訪問以前的同級元素,並且對於每個details元素,它正是其相關的subTitle

var topPlacesForCity = doc.Descendants("Destination") 
    .Where(x => (string) x.Element("City") == "Ahmedabad") 
    .Descendants("Top10PlacestoVisit") 
    .Elements("subTitle") 
    .Select(subTitle => new 
    { 
     SubTitle = subTitle.Value, 
     Details = (string) subTitle.ElementsAfterSelf("details").First() 
    }); 

如果,你在評論中提到,你可以改變這種摻入:

+0

這是什麼'XPathSelectElements'它並沒有爲我工作 – bill

+0

它位於'System.Xml.XPath'擴展方法。確保你有引用'System.Xml.Linq'程序集並在你的代碼中使用這個命名空間。 –

+0

@bill認爲我已經添加了''作爲父母。然後我可以這樣做。 – bill

0

嘗試像這樣,添加subTitleAttributedetails標籤這樣

<Top10PlacestoVisit> 

    <details subTitle ="place01">d1</details> 

</Top10PlacestoVisit> 

再這樣稱呼它。

XElement xe = XElement.Load(Server.MapPath("your path")); 

      var check = xe.Descendants("Destination") 
         .Where(n => n.Element("City").Value == "Ahmedabad") 
         .Select(l => l.Element("Top10PlacestoVisit")); 

      List<someList> dst = new List<someList>(); 
      foreach (var item in check) 
      { 
       var subs = item.Elements("details"); 
       foreach(var item1 in subs) 
       { 
        dst.Add(new someList 
        { 
         detail = item1.Value, 
         subtitle = item1.Attribute("subTitle").Value 

        }); 
       } 
      } 

      ViewBag.all = dst; 
相關問題