我想删除节点<Reference Include="System.Data" />
以编程方式从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>
我尝试使用此代码,但它无效:
XmlDocument doc = new XmlDocument();
doc.Load(fullFilePath);
XmlNode node = doc.SelectSingleNode(@"/Project/ItemGroup/Reference[@Include='System.Data']");
node.ParentNode.RemoveChild(node);
doc.Save(fullFilePath);
答案 0 :(得分:1)
如果SelectNodes或SingleSelectNodes不返回任何内容,则表示您的查询错误。在这种情况下,.csproj中的元素属于命名空间(此处声明为&#34;默认&#34;命名空间 - 没有前缀)
<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);
注意前缀&#34; p&#34;可以是任何东西,它只允许您在XPATH表达式中指定相应的命名空间,但是您需要它,即使在原始文档中它被声明为默认命名空间。
答案 1 :(得分:0)
使用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();
}
}
}