如何在c#中删除csproj文件中的节点?
我想删除节点如何在c#中删除csproj文件中的节点?,c#,.net,xml,csproj,C#,.net,Xml,Csproj,我想删除节点 以编程方式从csproj文件。 我的csproj文件结构如下: <ItemGroup> <Reference Include="Microsoft.CSharp" /> <Reference Include="System.Data.OracleClient" /> <Reference Include="System.Messaging" /> <Reference Include="Syste
以编程方式从csproj文件。
我的csproj文件结构如下:
<ItemGroup>
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data.OracleClient" />
<Reference Include="System.Messaging" />
<Reference Include="System.Web.DynamicData" />
<Reference Include="System.Web.Entity" />
<Reference Include="System.Web.ApplicationServices" />
<Reference Include="System.ComponentModel.DataAnnotations" />
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Core" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="System.Web.Extensions" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Drawing" />
<Reference Include="System.Web" />
<Reference Include="System.Xml" />
<Reference Include="System.Configuration" />
<Reference Include="System.Web.Services" />
<Reference Include="System.EnterpriseServices" />
</ItemGroup>
当您选择一个SelectNodes或SingleSelectNodes时,不返回任何内容,这意味着您的查询是错误的。在本例中,.csproj中的元素属于一个名称空间(这里声明为“默认”名称空间-没有前缀) 注意前缀“p”可以是任何名称,它只允许您在XPATH表达式中指定相应的名称空间,但您需要它,即使在原始文档中它被声明为默认名称空间。Easy with XML Linq
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string input =
"<Root>" +
"<ItemGroup>" +
"<Reference Include=\"Microsoft.CSharp\" />" +
"<Reference Include=\"System.Data.OracleClient\" />" +
"<Reference Include=\"System.Messaging\" />" +
"<Reference Include=\"System.Web.DynamicData\" />" +
"<Reference Include=\"System.Web.Entity\" />" +
"<Reference Include=\"System.Web.ApplicationServices\" />" +
"<Reference Include=\"System.ComponentModel.DataAnnotations\" />" +
"<Reference Include=\"System\" />" +
"<Reference Include=\"System.Data\" />" +
"<Reference Include=\"System.Core\" />" +
"<Reference Include=\"System.Data.DataSetExtensions\" />" +
"<Reference Include=\"System.Web.Extensions\" />" +
"<Reference Include=\"System.Xml.Linq\" />" +
"<Reference Include=\"System.Drawing\" />" +
"<Reference Include=\"System.Web\" />" +
"<Reference Include=\"System.Xml\" />" +
"<Reference Include=\"System.Configuration\" />" +
"<Reference Include=\"System.Web.Services\" />" +
"<Reference Include=\"System.EnterpriseServices\" />" +
"</ItemGroup>" +
"</Root>";
XDocument doc = XDocument.Parse(input);
List<XElement> itemGroup = doc.Descendants("ItemGroup").ToList();
itemGroup.Elements("Reference").Where(x => x.Attribute("Include").Value == "System.Data").Remove();
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Xml;
使用System.Xml.Linq;
命名空间控制台应用程序1
{
班级计划
{
静态void Main(字符串[]参数)
{
字符串输入=
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"";
XDocument doc=XDocument.Parse(输入);
List itemGroup=doc.substands(“itemGroup”).ToList();
itemGroup.Elements(“Reference”).Where(x=>x.Attribute(“Include”).Value==“System.Data”).Remove();
}
}
}
“它不工作”永远都是不够的信息。当你尝试时会发生什么?(我怀疑问题在于您忽略了元素的名称空间,这是根元素的xmlns=…
部分默认的名称空间。我还建议使用LINQ to XML作为通常更好的API,但这是另一回事。)您应该使用XmlNamespaceManager。乔恩·斯基特被发现了!他才1岁!!
<Project ... xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
...
</Project>
XmlDocument doc = new XmlDocument();
XmlNamespaceManager nsmgr = new XmlNamespaceManager(new NameTable());
nsmgr.AddNamespace("p", "http://schemas.microsoft.com/developer/msbuild/2003");
doc.Load(fullFilePath);
XmlNode node = doc.SelectSingleNode(@"/p:Project/p:ItemGroup/p:Reference[@Include='System.Data']", nsmgr);
node.ParentNode.RemoveChild(node);
doc.Save(fullFilePath);
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string input =
"<Root>" +
"<ItemGroup>" +
"<Reference Include=\"Microsoft.CSharp\" />" +
"<Reference Include=\"System.Data.OracleClient\" />" +
"<Reference Include=\"System.Messaging\" />" +
"<Reference Include=\"System.Web.DynamicData\" />" +
"<Reference Include=\"System.Web.Entity\" />" +
"<Reference Include=\"System.Web.ApplicationServices\" />" +
"<Reference Include=\"System.ComponentModel.DataAnnotations\" />" +
"<Reference Include=\"System\" />" +
"<Reference Include=\"System.Data\" />" +
"<Reference Include=\"System.Core\" />" +
"<Reference Include=\"System.Data.DataSetExtensions\" />" +
"<Reference Include=\"System.Web.Extensions\" />" +
"<Reference Include=\"System.Xml.Linq\" />" +
"<Reference Include=\"System.Drawing\" />" +
"<Reference Include=\"System.Web\" />" +
"<Reference Include=\"System.Xml\" />" +
"<Reference Include=\"System.Configuration\" />" +
"<Reference Include=\"System.Web.Services\" />" +
"<Reference Include=\"System.EnterpriseServices\" />" +
"</ItemGroup>" +
"</Root>";
XDocument doc = XDocument.Parse(input);
List<XElement> itemGroup = doc.Descendants("ItemGroup").ToList();
itemGroup.Elements("Reference").Where(x => x.Attribute("Include").Value == "System.Data").Remove();
}
}
}