从 xml 文件中删除标签

Posted

技术标签:

【中文标题】从 xml 文件中删除标签【英文标题】:Remove Tag from xml file 【发布时间】:2021-04-26 02:53:54 【问题描述】:

如何删除 workbookProtection 标签?

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="x15" xmlns:x15="http://schemas.microsoft.com/office/spreadsheetml/2010/11/main">
  <fileVersion appName="xl" lastEdited="6" lowestEdited="6" rupBuild="14420" />
  <workbookPr filterPrivacy="1" codeName="ThisWorkbook" defaultThemeVersion="164011" />
  <workbookProtection workbookAlgorithmName="SHA-512" workbookHashValue="MI+PN5CyUQ3XO6V0pjh3peL3nUtsQcVWhtDfT6PQjyrHvEBu9Hk+dzFJxHm3V5vxGgtgMk1eLpi62pzDLJ9Y4w==" workbookSaltValue="yhbUOo6A+kVhRScY5lXa3g==" workbookSpinCount="100000" lockStructure="1" />
  <bookViews>
    <workbookView xWindow="-120" yWindow="-120" windowWidth="21840" windowHeight="13140" tabRatio="658" />
  </bookViews>
 ...
</workbook>

【问题讨论】:

这能回答你的问题吗? How to delete node from XML file using C# @tnw no 我测试过但没用 显示您目前的代码,然后告诉我们您遇到了什么具体问题。 @tnw XDocument doc = XDocument.Load(path); var q = from node in doc.Descendants("workbookProtection") 选择节点; q.ToList().ForEach(x => x.Remove()); doc.Save("output.xml"); 【参考方案1】:

您应该在尝试获取workbookProtection 时添加命名空间,方法是使用Linq to Xml

1 - 声明命名空间:

XNamespace xn = "http://schemas.openxmlformats.org/spreadsheetml/2006/main";

2 - 删除 workbookProtection

XDocument doc = XDocument.Load(path);
var q = from node in doc.Descendants(xn + "workbookProtection")
        select node;
q.ToList().ForEach(x => x.Remove());

测试用:

string xml = @"
<workbook xmlns=""http://schemas.openxmlformats.org/spreadsheetml/2006/main"" xmlns:r=""http://schemas.openxmlformats.org/officeDocument/2006/relationships"" xmlns:mc=""http://schemas.openxmlformats.org/markup-compatibility/2006"" mc:Ignorable=""x15"" xmlns:x15=""http://schemas.microsoft.com/office/spreadsheetml/2010/11/main"">
  <fileVersion appName=""xl"" lastEdited=""6"" lowestEdited=""6"" rupBuild=""14420"" />
  <workbookPr filterPrivacy=""1"" codeName=""ThisWorkbook"" defaultThemeVersion=""164011"" />
  <workbookProtection workbookAlgorithmName=""SHA-512"" workbookHashValue=""MI+PN5CyUQ3XO6V0pjh3peL3nUtsQcVWhtDfT6PQjyrHvEBu9Hk+dzFJxHm3V5vxGgtgMk1eLpi62pzDLJ9Y4w=="" workbookSaltValue=""yhbUOo6A+kVhRScY5lXa3g=="" workbookSpinCount=""100000"" lockStructure=""1"" />
  <bookViews>
    <workbookView xWindow=""-120"" yWindow=""-120"" windowWidth=""21840"" windowHeight=""13140"" tabRatio=""658"" />
  </bookViews>
</workbook>";

XNamespace xn = "http://schemas.openxmlformats.org/spreadsheetml/2006/main";

XDocument doc = XDocument.Parse(xml);
var q = from node in doc.Descendants(xn + "workbookProtection")
        select node;
q.ToList().ForEach(x => x.Remove());

Console.WriteLine(doc);

结果

<workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmln
s:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:
mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="x
15" xmlns:x15="http://schemas.microsoft.com/office/spreadsheetml/2010/11/main">
  <fileVersion appName="xl" lastEdited="6" lowestEdited="6" rupBuild="14420" />
  <workbookPr filterPrivacy="1" codeName="ThisWorkbook" defaultThemeVersion="164
011" />
  <bookViews>
    <workbookView xWindow="-120" yWindow="-120" windowWidth="21840" windowHeight
="13140" tabRatio="658" />
  </bookViews>
</workbook>

希望对您有所帮助。

【讨论】:

不客气!试试doc.Descendants(xn + "workbookProtection").Remove(); 也可以。

以上是关于从 xml 文件中删除标签的主要内容,如果未能解决你的问题,请参考以下文章

从 xml 片段中删除 html 标签? [复制]

如何使用 XSLT 从 XML 中删除元素标记

使用 sed 从类似 XML 的文件中删除特定文本

如何从 XML 文件中提取所有标签及其子项?

从 XML 文件 (Java) 中的特定标签解析内容

从 zip 文件中读取的 XML 文件中删除空格