【问题标题】:Add an element to xml file将元素添加到 xml 文件
【发布时间】:2010-01-06 13:24:49
【问题描述】:

我正在尝试在 C# .csproj 文件中添加和删除元素。该文件部分显示在下面。有人可以告诉我如何做以下两件事吗?

  1. 如下图所示添加一个元素(“我想添加 这个")
  2. 删除一个元素。例如,假设我想删除我的行 已在下面指出。
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="3.5" DefaultTargets="Build" 
xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
   <PropertyGroup>
      <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
</PropertyGroup>
<ItemGroup>
   <Reference Include="System.Data" />    
   <Reference Include="System.Deployment" />
</ItemGroup>
<ItemGroup>
   <Compile Include="Generate\DatabaseContext.cs" />
   <Compile Include="Generate\EntityClasses.cs" />
   <Compile Include="Generate\Extensions.cs" />
   <Compile Include="Schema\Column.cs" />
   <Compile Include="Schema\EntityRef.cs" />
   <Compile Include="SerializedData\Tables.xml" />  //I want to add this
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

【问题讨论】:

  • 您是否正在寻找如何添加元素,或者如何将其添加到特定的 ItemGroup 节点?
  • 我想添加一个特定的 ItemGroup 节点。我已经标记了我想添加的确切节点(请参阅“我想添加这个”的行)。

标签: c# .net xml linq linq-to-xml


【解决方案1】:

您可以通过以下方式添加您指定的行:

XNamespace ns = "http://schemas.microsoft.com/developer/msbuild/2003";
XDocument xDoc = XDocument.Load(fileName);

var b = xDoc.Descendants(ns + "Compile").First();

b.Parent.Add(
    new XElement(ns + "Compile", 
        new XAttribute("Include", @"SerializedData\Tables.xml")
    )
);

xDoc.Save(fileName);

要删除您指定的行,请尝试以下操作:

XNamespace ns = "http://schemas.microsoft.com/developer/msbuild/2003";
XDocument xDoc = XDocument.Load(fileName);

var b = xDoc.Descendants(ns + "Compile")
    .Where(el => el.Attribute("Include").Value == @"SerializedData\Tables.xml");

if (b != null)
{
    b.Remove();
    xDoc.Save(fileName);
}

【讨论】:

    【解决方案2】:

    我觉得应该没问题

    XDocument xmlDoc = XDocument.Load(Server.MapPath("People.xml"));
    
    xmlDoc.Element("Persons").Add(new XElement("Person", new XElement("Name", txtName.Text),
    new XElement("City", txtCity.Text), new XElement("Age", txtAge.Text)));
    
    xmlDoc.Save(Server.MapPath("People.xml"));
    

    【讨论】:

    • 谢谢。如果这个答案适合我的例子,它会更有用。
    【解决方案3】:
            XDocument projects = XDocument.Load(fileName);
            XNamespace xmlns = "http://schemas.microsoft.com/developer/msbuild/2003";
    
            // Delete element (<Compile Include="SerializedData\Tables.xml" />);
            var query1 = from p in projects.Descendants(xmlns + "Project").Descendants(xmlns + "ItemGroup").Descendants(xmlns + "Compile") 
                         where p.Attribute("Include").Value == @"SerializedData\Tables.xml" select p;
            if (query1.Any())
            {
                XElement node = query1.Single();
                node.Remove(); 
            }
    
            //System.Diagnostics.Debug.WriteLine(projects);
            projects.Save(fileName);
    
            // Add the element.
            var query2 = from p in projects.Descendants(xmlns + "Project").Descendants(xmlns + "ItemGroup") where p.Descendants(xmlns + "Compile").Any() select p;
            if (query2.Any())
            {
                query2.Single().Add(new XElement(xmlns + "Compile", new XAttribute("Include", @"SerializedData\Tables.xml")));  
            }
            //System.Diagnostics.Debug.WriteLine(projects);
            projects.Save(fileName);
    

    【讨论】: