Alexander
Alexander

Reputation: 51

Delete xml tags variables in xml file with C#

I have xml file that has tags <File> I need to delete some variables and its values from these tags. version="$(Version_Infralution.Common.dll)">Infralution.Common.dll and all variables versions and its values. How can I do it in C#?

Part of XML file contents:

 <File version="$(Version_Infralution.Common.dll)">Infralution.Common.dll</File> 
 <File version="$(Version_Infralution.Common.dll)">Infralution.Controls.dll</File>
 <File version="$(Version_Infralution.Common.dll)">Infralution.Controls.VirtualTree.dll</File>
 <File size="73728">Infralution.RichText.dll</File>
 <File version="$(Version_Interop.DSOFile.dll)">Interop.DSOFile.dll</File>
 <File version="$(Version_NLog.dll)">NLog.dll</File>

Sample result:

 <File>Infralution.Common.dll</File> 
 <File>Infralution.Controls.dll</File>
 <File>Infralution.Controls.VirtualTree.dll</File>
 <File size="73728">Infralution.RichText.dll</File>
 <File>Interop.DSOFile.dll</File>
 <File>NLog.dll</File>

XML file structure has a lot of child tags before tag, ex:

<Products>
    <Product name="Connectors">
      <Registry>
        <Reg key="HKEY_CURRENT_USER\Software\ScanJour\iBox\Install" value_name="SettingsEditorShortcuts" value="1" platform="x64" />
      </Registry>
      <SharedProductRef name="SharedProduct for: ModelBuilder Client, iBox Search, Connectors" />
      <SharedProductRef name="SharedProduct for: ModelBuilder Client, iBox Server\iBox Utilities, iBox Server, iBox Server\ADODBC Manager, iBox Search, Connectors\Connector Manager, Connectors" />
      <SharedProductRef name="SharedProduct for: SharePoint Server Add-on\Search Control Webpart, Connectors" />
    </Product>
    <Product name="Connectors\Connector Manager">
      <FileSystem>
        <Dir name="ProgramFilesX64" value="ScanJour\iBox\Common Components\ConnectorManager\">
          <File version="$(Version_CSScriptLibrary.v2.0.dll)">CSScriptLibrary.v2.0.dll</File>
          <File version="$(Version_Infralution.Common.dll)">Infralution.Common.dll</File>
          <File version="$(Version_Infralution.Common.dll)">Infralution.Controls.dll</File>

Upvotes: 0

Views: 1136

Answers (2)

Alexander
Alexander

Reputation: 51

I am answering my own question (maybe someone needs this solution also) Working code:

    String path = @"C:\iBoxProductValidator.xml";
    String pathResult = @"C:\iBoxProductValidatorResult.xml";

    XmlDocument configDoc = new XmlDocument();

    configDoc.Load(path);

    XmlNodeList projectNodes = configDoc.GetElementsByTagName("File");

    for (int i = 0; i < projectNodes.Count; i++)
    {
        if (projectNodes[i].Attributes["version"] != null)
        {
            projectNodes[i].Attributes.Remove(projectNodes[i].Attributes["version"]);
        }
    }

    configDoc.Save(pathResult);

Upvotes: 0

mellamokb
mellamokb

Reputation: 56779

You can modify XML easily with LINQ-to-XML. First parse the source document into an XDocument object (you can load files with .Load, or process a string variable containing XML with .Parse):

var xdoc = XDocument.Load("/path/to/filename.xml");

You can remove the nodes you don't want by filtering for the specific nodes and using the .Remove extension method (this example removes any element of type <File> that has an attribute version with an exact value of $(Version_Infralution.Common.dll) - you can chain multiple conditions if you want to validate other constraints as well):

xdoc.Descendants("File")
    .Where(x =>
        x.Attribute("version") != null &&
        x.Attribute("version").Value == "$(Version_Infralution.Common.dll)")
    .Remove();

Sample Result:

<Files>
  <File size="73728">Infralution.RichText.dll</File>
  <File version="$(Version_Interop.DSOFile.dll)">Interop.DSOFile.dll</File>
  <File version="$(Version_NLog.dll)">NLog.dll</File>
</Files>

You can also alter specific nodes, for example changing the contents of the node, or values of specific attributes, or removing attributes altogether - this example removes the version attribute from any <File> element having a version of "$(Version_Infralution.Common.dll)":

foreach (var xn in xdoc.Descendants("File")) {
    if (xn.Attribute("version") != null &&
          xn.Attribute("version").Value == "$(Version_Infralution.Common.dll)") {
        xn.Attribute("version").Remove();
    }
}

Sample Result:

<Files>
  <File>Infralution.Common.dll</File>
  <File>Infralution.Controls.dll</File>
  <File>Infralution.Controls.VirtualTree.dll</File>
  <File size="73728">Infralution.RichText.dll</File>
  <File version="$(Version_Interop.DSOFile.dll)">Interop.DSOFile.dll</File>
  <File version="$(Version_NLog.dll)">NLog.dll</File>
</Files>

Finally, you can save the result to file with .Save:

xdoc.Save("/path/to/newfilename.xml");

Upvotes: 1

Related Questions