Tuesday 25 September 2012

Edit csproj.cs file programmatically using c#

private void IncludeFilesInProject(string csProjName, string tagContent)
        {
            string csprojPath = txtdefaultPath.Text + "\\" + csProjName;
            XmlDocument myXmlDocument = new XmlDocument();

            myXmlDocument.Load(csprojPath);

            XmlTextReader reader = new XmlTextReader(csprojPath);
            XmlDocument doc = new XmlDocument();

            doc.Load(reader);
            reader.Close();
            XmlNode currNode;

            XmlDocumentFragment docFrag = doc.CreateDocumentFragment();


            docFrag.InnerXml = tagContent;

            // insert the availability node into the document
            currNode = doc.DocumentElement;
            currNode.InsertAfter(docFrag, currNode.LastChild);
           
            //save the output to a file
            doc.Save(csprojPath);

            //Overwrite the existing file with new file content.
            string projFile = File.ReadAllText(csprojPath);
            string newProjFile = projFile.Replace(" xmlns=\"\"", "");
            File.WriteAllText(csprojPath, newProjFile);
        }

No comments:

Post a Comment

How to find a string within a jQuery or javascript string

Sometimes, you required to find a the existence of a small string with in a string. This article will  demonstarte , how could you do by...