我有一个问题。
仍在开发一个项目,但我陷入困境。
我们有这样的代码:
var copyitems = doc.Descendants(application) // Read all descendants
.SelectMany(x => x.Elements("Test"))
.Select(s =>
{
var splits = s.Value.Split(new string[] { "@SRCDIR@", "@INSTDIR@" }, StringSplitOptions.RemoveEmptyEntries); // split the string to separate source and destination.
return new { Source = splits[0].Replace(",", ""), Destination = splits[1].Replace(",", "") };
})
.ToList();
结合此 xml 示例:
<root>
<Test>
<Copy>@SRCDIR@\test1 ,@INSTDIR@\test1</Copy>
</Test>
<root>
但是现在 xml 变成了这样:
<root>
<Test>
<Copy>
<Source>@SRCDIR@\test</Source>
<Destination>@INSTDIR@\test1</Destination>
</Copy>
</Test>
<root>
我现在如何做同样的事情,而不是进行分割,只需读取源和目标的信息。
您还可以拥有多个副本后代以及具有其他名称但没有源值和目标值的后代
最佳答案
查找接受元素名称作为输入的 Element
方法。
var copyitems = doc.Descendants(application) // Read all descendants
.SelectMany(x => x.Elements("Test"))
.Select(s =>
{
return new
{
Source = s.Element("Source").Value.Replace("@SRCDIR@", ""),
Destination =s.Element("Destination").Value.Replace("@INSTDIR@", "")
};
})
.ToList();
关于C# Linq 获取后代的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37805054/