【问题标题】:Delete xml tags variables in xml file with C#使用C#删除xml文件中的xml标签变量
【发布时间】:2013-04-24 13:40:05
【问题描述】:

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

部分XML文件内容:

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

【问题讨论】:

    标签: c# xml tags


    【解决方案1】:

    您可以使用LINQ-to-XML 轻松修改 XML。首先将源文档解析成XDocument对象(可以用.Load加载文件,也可以用.Parse处理包含XML的字符串变量):

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

    您可以通过过滤特定节点并使用 .Remove 扩展方法来删除不需要的节点(此示例删除任何类型为 &lt;File&gt; 且具有属性 version 且精确值为$(Version_Infralution.Common.dll) - 如果您还想验证其他约束,您可以链接多个条件):

    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>
    

    您还可以更改特定节点,例如更改节点的内容或特定属性的值,或完全删除属性 - 此示例从任何版本为 @987654339 的 &lt;File&gt; 元素中删除 version 属性@987654339 @:

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

    【讨论】:

    • 任务是仅删除属性版本及其所有值。并留下标签文件及其内容。因此,如果我有行 - NLog.dll 我需要获得行 NLog.dll.
    • @Alexander:查看更新。我添加了示例代码,如何修改现有节点以使用 foreach 迭代器循环删除属性。有一些直观的方法可以非常轻松地更改、删除或添加属性、标签和值。
    • 您的解决方案很酷而且很有帮助,但解决了不同的问题。我只需要删除版本属性及其值,而不是删除包含具有特定属性值的属性的整个标签
    • @Alexander:有很多关于 LINQ-to-XML 中可用的方法和属性的文档。您可能需要进行一些探索和研究,但这应该会让您走上正轨。事实上,我编写上面的大部分代码只是简单地使用 Visual Studio Intellisense 来查看可用的方法,根据名称推断它们的作用,然后尝试一下。你也可以这样做。
    • 我正在研究所有这些方法,但无法解决:(将继续尝试......
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2021-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-10
      • 2021-01-24
      相关资源
      最近更新 更多