Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 要选择xml linq c中的所有子代节点吗#_C#_Asp.net_Xml - Fatal编程技术网

C# 要选择xml linq c中的所有子代节点吗#

C# 要选择xml linq c中的所有子代节点吗#,c#,asp.net,xml,C#,Asp.net,Xml,我有以下xml,并试图选择房间节点内的所有节点问题是房间节点内的节点名称几乎是随机的,因此不可能预测 <room> <info>1</info> <something_here>1</something_here> <something_else_here>no</something_else_here> <something_else_here>no</something_else_here&

我有以下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>

创建列表时,务必运行此
p.Element(“尝试”)!=在调用此
p.Element(“尝试”).Value==“否”
之前,为null
,因此如果
p.Element(“尝试”)
不存在,则不会引发错误

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
XDocument thedocument=XDocument.Load(@“M:\StackOverflowQuestionsAndAnswers\xml42699433\xml42699433\file.XML”);
列出listfvalues=doc.substands(“房间”)
其中(p=>p.Element(“尝试”)!=null&&p.Element(“尝试”)。值==“否”)
.要素()
.Select(p=>p.Value).ToList();
string asLongString=string.Join(“,”,thelistfvalues);
//让它成为你想要的绳子

listfvalues
仅包含在
i.Element(“尝试”)中找到的值。Value
i.Element(“尝试”)。如果在xml中找不到元素,Value
将返回错误(null),而
(string)i.Element(“尝试”)
将不返回错误。您需要检查元素是否为null仍然不工作。它返回11nononono09/03/20174.23作为一个字符串,我希望至少用逗号1,1,no,no,no,no,09/03/2017,4.23分隔它们,或者对每个值随时以不同的字符串分隔。我将添加一些注释和
string.join()
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