簡體   English   中英

想要在xml linq C#中選擇所有后代節點

[英]want to select all the descendant nodes in xml linq c#

我有下面的xml並嘗試選擇房間節點內的所有節點問題是房間節點內的節點名稱幾乎是隨機的,因此無法預測。

<room>
<info>1</info>
<something_here>1</something_here>
<something_else_here>no</something_else_here>
<something_else_here>no</something_else_here>
<something_else>no</something_else>
<attempt>no</attempt>
<date>09/03/2017</date>
<room_name>4.23</room_name>

我已經嘗試過了,但是它將所有后代節點房間信息作為一個字符串返回,我希望該信息用字符串分隔

tabela = xdoc.Descendants("room")
            .Where(i => (string)i.Element("attempt") == Convert.ToString("no"))
           .Select(element => element.Value).ToList();

創建列表時,在調用此p.Element("attempt").Value == "no"之前,請運行此p.Element("attempt") != null很重要,因此,如果p.Element("attempt")不存在。

XDocument thedoc = XDocument.Load(@"M:\StackOverflowQuestionsAndAnswers\XML_42699433\XML_42699433\file.xml");
List<string> theListOfValues = thedoc.Descendants("room")
.Where(p => p.Element("attempt") != null && p.Element("attempt").Value == "no")
.Elements()
.Select(p => p.Value).ToList();
string asLongString = string.Join(",", theListOfValues);
//make it the string you seem to be after

theListOfValues僅包含在其中找到的值

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM