Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/260.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/3/xpath/2.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# 在XPath数组中串联内部文本_C#_Xpath_Linq To Xml - Fatal编程技术网

C# 在XPath数组中串联内部文本

C# 在XPath数组中串联内部文本,c#,xpath,linq-to-xml,C#,Xpath,Linq To Xml,我有一些XML如下所示: <root> <instructions type="array"> <item type="string">Cannot do business directly with consumers</item> <item type="string">Cannot marry a martian</item> </instructions> </root>

我有一些XML如下所示:

<root>
  <instructions type="array">
    <item type="string">Cannot do business directly with consumers</item>
    <item type="string">Cannot marry a martian</item>
  </instructions>
</root>
var instructions = myXDocument.XPathSelectElements("/root/instructions").Nodes();
我试图连接所有
字符串,因此:

“不能直接与消费者做生意,不能与火星人结婚”

到目前为止,我的尝试是

instructions.Select(x => x.[[What do I put here?]]).Aggregate((i,j) => i + ", " + j)

但是我很难弄清楚如何从lambda表达式中的每个节点获取内部文本
x.ToString()
产生
“无法直接与消费者做生意”

我知道这不是您要寻找的lambda表达式,但这是获得结果输出的方法

string result;
XElement root = XElement.Load(SomeXML);
root.Element("root").Elements("instructions").Elements("item").All<XElement>(xe =>
{
    result = result + xe.Attribute("type").Value),
    return true;
});
字符串结果;
XElement root=XElement.Load(SomeXML);
root.Element(“根”).Elements(“说明”).Elements(“项”).All(xe=>
{
结果=结果+xe.Attribute(“type”).Value),
返回true;
});
试试这个

var xml = "<root><instructions type=\"array\"><item type=\"string\">Cannot do business directly with consumers</item><item type=\"string\">Cannot marry a martian</item></instructions></root>";
var document = XDocument.Parse(xml);
var result = string.Join(", ", document.Descendants("instructions").Elements("item").Select(x=>x.Value));
var xml=“不能直接与消费者做生意不能与火星人结婚”;
var document=XDocument.Parse(xml);
var result=string.Join(“,”,document.subjects(“指令”).Elements(“项目”).Select(x=>x.Value));
输出:

不能直接与消费者做生意,不能与火星人结婚


使用相同的方法,您只需将
节点()
替换为
元素()
,然后访问返回的
元素的
属性以获取内部文本:

var instructions = myXDocument.XPathSelectElements("/root/instructions").Elements();
var result = instructions.Select(x => x.Value).Aggregate((i,j) => i + ", " + j);