如何在C#中获取xml父元素中的所有子元素 [英] How to get all child elements in the parent element of xml in c#

查看:167
本文介绍了如何在C#中获取xml父元素中的所有子元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

认为这是我的xml ..

think this is my xml ..

<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都列出来.我该怎么做.这就是我尝试过的.

so I want to get all subTitle and details to a list.how can I do that.This is what I tried.

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>

in this place get all the data(subTitle and details) inside the <Top10PlacestoVisit></Top10PlacestoVisit>

然后我创建了Model并分配了这样的值.但是我无法立即将subTitledetails进入列表.这是我尝试过的

then I created a Model and assigned values like this.but I can't get subTitle and details at once to the list.this is what I tried

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;

在这里它成功获取subTitle,但是我如何同时获取details并进行分配.请帮帮我.

in here it successfully gets subTitle but how can I get details and assign it in the same time.help me with this.

推荐答案

如果您坚持使用XML,则可以通过使用如下所示的结构来实现:

If you're stuck with your XML, then you can do it by assuming the structure using something like this:

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()
    });

如果正如您在评论中提到的那样,可以更改它以包含父Place元素,那么它会更健壮和明显:

If, as you mention in the comments, you can change this to incorporate a parent Place element, then it's a little more robust and obvious:

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("...")).

As an aside, there is no reason to load your XML into XmlDocument to then just parse it straight into XDocument and throw it away. Use XDocument.Load(Server.MapPath("...")) instead.

这篇关于如何在C#中获取xml父元素中的所有子元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆