2010-01-06 87 views
2

我想添加和刪除C#.csproj文件中的元素。該文件部分出現在下面。有人能告訴我如何做以下兩件事嗎?添加元素到xml文件

  1. 添加元素,如下圖所示(即說:「我想補充 這個」行)
  2. 刪除的元素。例如,假設我想刪除下面指示的行I 。
<?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> 
+0

您是否在尋找如何添加元素,或者如何將其添加到特定的ItemGroup節點? – 2010-01-06 15:14:55

+0

我想添加一個特定的ItemGroup節點。我標記了我想要添加的確切節點(請參閱「我想添加此內容」一行)。 – 2010-01-06 15:26:57

回答

4

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

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); 
} 
1

我思這應該是罰款

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")); 
+0

謝謝。如果它適合我​​的例子,這個答案會更有用。 – 2010-01-06 13:58:02

1
 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);