c# - 从 xml 文件中删除标签
问题描述
如何删除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>
解决方案
当您尝试workbookProtection
使用Linq to Xml获取时,您应该添加命名空间:
1 - 声明命名空间:
XNamespace xn = "http://schemas.openxmlformats.org/spreadsheetml/2006/main";
2 - 删除工作簿保护
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>
我希望你觉得这有帮助。
推荐阅读
- android - 以编程方式打开首选项
- python - TensorFlow 服务与 contrib 操作
- json - 如何在 Jackson 中以特定格式显示小数(对于 JSON)
- ios - 如何从非越狱设备中提取 dyld_shared_cache?
- ios - SpriteKit 暂停场景 (Swift4.2)
- java - 抽象超类中的受保护字段是否应该在子类中使用 super 或 this 访问?
- xml - sed 用于搜索和替换 xml 文件中的文本
- reactjs - 如何在 React 中使用 Redux
- c++ - c++ 快速排序向量对作为遗传学习算法的一部分
- c - 将文件字符转换为大写