删除XML文件中的xml标签变量与C#

问题描述:

我有xml文件,有标签<File>我需要从这些标签中删除一些变量及其值。 version="$(Version_Infralution.Common.dll)">Infralution.Common.dll和所有变量版本及其值。我怎样才能在C#中做到这一点?的XML文件内容删除XML文件中的xml标签变量与C#

部分:

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

样品结果:

<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文件的结构有很多标记之前,前童标签:

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

你可以用LINQ-to-XML轻松修改XML。首先分析源文档转换为XDocument对象(你可以将文件加载与.Load,或处理含有字符串变量XML与.Parse):

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

可以通过为特定的过滤删除不想要的节点节点并使用.Remove扩展方法(本实施例中删除了具有的$(Version_Infralution.Common.dll)的精确值的属性version<File>类型的任何元件 - 可以链中的多个条件,如果要验证其它约束以及):

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

样品结果:

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

也可以改变特定节点,例如改变该节点的内容,或特定属性的值,或者完全去除属性 - 这个例子从任何删除version属性<File>元件具有版本的"$(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(); 
    } 
} 

样品结果:

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

最后,你可以保存结果与.Save到文件:

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

任务是仅删除属性版本及其所有值。并留下标签文件及其内容。因此,如果我有行 - NLog.dll我需要得到行 NLog.dll。 – Alexander 2013-04-24 14:03:53

+0

@Alexander:查看更新。我添加了示例代码,如何修改现有节点以使用'foreach'迭代器循环移除属性。有直观的方法可以非常容易地更改,删除或添加属性,标记和值。 – mellamokb 2013-04-24 14:05:40

+0

您的解决方案很酷且有帮助,但解决了不同的问题。我需要仅删除版本属性及其值,而不是删除包含具有特定属性值的属性的整个标记 – Alexander 2013-04-24 14:16:43

我回答我的问题(也许有人需要这个解决方案还) 工作代码:

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