我有跟随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();
答案 0 :(得分:2)
创建列表时,在调用此p.Element("attempt") != null
之前运行此p.Element("attempt").Value == "no"
非常重要,这样如果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
仅包含