I wrote a code generator that generates C # files. If the file you are creating is new, I need to add a link to it in our .csproj file. I have the following method that adds node to a .csproj file.
private static void AddToProjectFile(string projectFileName, string projectFileEntry)
{
StreamReader streamReader = new StreamReader(projectFileName);
XmlTextReader xmlReader = new XmlTextReader(streamReader);
XElement element;
XNamespace nameSpace;
XDocument xmlDoc = XDocument.Load(xmlReader);
nameSpace = xmlDoc.Root.Name.Namespace;
streamReader.Close();
element = new XElement(nameSpace + "Compile", new XAttribute("Include", projectFileEntry));
xmlDoc.Root.Elements(nameSpace + "ItemGroup").ElementAt(1).Add(element);
xmlDoc.Save(projectFileName);
}
This method works just fine. However, it does not add node to the new line. It will add it to the previous line in the .csproj file. This leads to some confusion when merging TFS. How to add a new node to a new line?
source
share