基于问题:Reading a *.CSPROJ file in C#
我有代码从* .csproj文件中提取一些属性,类似于:
Project project = new Project();
var Property001=
from pg in project.PropertyGroups.Cast<BuildPropertyGroup>()
from item in pg.Cast<BuildProperty>()
where item.Name == "Property001"
select item.Value.ToString();
这很好用,但接下来的问题是如何使用LINQ更新属性?
答案 0 :(得分:1)
您可以使用LINQ来获取属性项 - 而不仅仅是值 - 来更新:
var Property001item =
(from pg in project.PropertyGroups.Cast<BuildPropertyGroup>()
from item in pg.Cast<BuildProperty>()
where item.Name == "Property001"
select item).FirstOrDefault();
if (Property001item != null)
{
Property001item.Value = "MyNewValue";
}