【问题标题】:How to get all child elements in the parent element of xml in c#如何在c#中获取xml的父元素中的所有子元素
【发布时间】:2016-01-22 07:23:51
【问题描述】:

认为这是我的 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 都列在一个列表中。我该怎么做。这就是我尝试过的。

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

在这里获取&lt;Top10PlacestoVisit&gt;&lt;/Top10PlacestoVisit&gt;中的所有数据(子标题和详细信息)

然后我创建了一个Model 并分配了这样的值。但是我无法同时将subTitledetails 加入到列表中。这就是我尝试过的

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并同时分配它。帮我解决这个问题。

【问题讨论】:

    标签: c# .net xml linq-to-xml


    【解决方案1】:

    如果您对自己的 XML 感到困惑,那么您可以通过使用以下方式假设结构来做到这一点:

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

    如果正如您在 cmets 中提到的那样,您可以将其更改为包含父 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("..."))

    【讨论】:

      【解决方案2】:

      您的 xml 看起来结构不太好。从我的角度来看,将与之相关的 subTitledetails 放入一些封闭的父元素中是合理的,例如:

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

      无论如何,如果您被您发布的结构所束缚并且无法更改它 - 您仍然能够实现您的目标,例如:

      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 元素的集合。因此,您只是使用XElementPreviousNode 属性访问前一个同级元素,并且对于每个details 元素,它正是其相关的subTitle

      【讨论】:

      • 这是什么XPathSelectElements 它对我不起作用
      • 是位于System.Xml.XPath的扩展方法。确保您有引用 System.Xml.Linq 程序集并在您的代码中使用此命名空间。
      • @bill 认为我已将 &lt;place&gt;&lt;/place&gt; 添加为父母。那么我可以这样做吗。
      【解决方案3】:

      像这样尝试,像这样在details 标签内添加subTitle 作为Attribute

      <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;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-06-13
        • 2021-12-25
        • 2019-02-19
        • 2014-11-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多