2

I am trying to add and delete elements from a C# .csproj file. The file, in part, appears below. Can someone show me how I can do the following two things?

  1. Add an element as shown below (the line that says, "I want to add this")
  2. Delete an element. For example, say I wanted to delete the line I have indicated below.
<?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> 
2
  • Are you looking for how to add the element at all, or how to add it to the specific ItemGroup node? Commented Jan 6, 2010 at 15:14
  • I would like to add a specific ItemGroup node. I've marked the exact node I would like to add (see the line that says "I want to add this"). Commented Jan 6, 2010 at 15:26

3 Answers 3

4

You can add your indicated line in the following way:

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

To remove your indicated line, try this:

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); } 
Sign up to request clarification or add additional context in comments.

Comments

1

Ithink this should be fine

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

1 Comment

Thank you. This answer would be more useful if it was adapted to my example.
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); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.