J’essaie d’append et de supprimer des éléments d’un fichier C # .csproj. Le fichier, en partie, apparaît ci-dessous. Est-ce que quelqu’un peut me montrer comment je peux faire les deux choses suivantes?
Debug //I want to add this
Vous pouvez append votre ligne indiquée de la manière suivante:
XNamespace ns = "http://schemas.microsoft.com/developer/msbuild/2003"; XDocument xDoc = XDocument.Load(fileName); var b = xDoc.Descendants(ns + "Comstack").First(); b.Parent.Add( new XElement(ns + "Comstack", new XAtsortingbute("Include", @"SerializedData\Tables.xml") ) ); xDoc.Save(fileName);
Pour supprimer votre ligne indiquée, essayez ceci:
XNamespace ns = "http://schemas.microsoft.com/developer/msbuild/2003"; XDocument xDoc = XDocument.Load(fileName); var b = xDoc.Descendants(ns + "Comstack") .Where(el => el.Atsortingbute("Include").Value == @"SerializedData\Tables.xml"); if (b != null) { b.Remove(); xDoc.Save(fileName); }
Je pense que ça devrait aller
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"));
XDocument projects = XDocument.Load(fileName); XNamespace xmlns = "http://schemas.microsoft.com/developer/msbuild/2003"; // Delete element ( ); var query1 = from p in projects.Descendants(xmlns + "Project").Descendants(xmlns + "ItemGroup").Descendants(xmlns + "Comstack") where p.Atsortingbute("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 + "Comstack").Any() select p; if (query2.Any()) { query2.Single().Add(new XElement(xmlns + "Comstack", new XAtsortingbute("Include", @"SerializedData\Tables.xml"))); } //System.Diagnostics.Debug.WriteLine(projects); projects.Save(fileName);