Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
165 views
in Technique[技术] by (71.8m points)

c# - Remove Tag from xml file

How can I remove the workbookProtection tag?

<?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>
question from:https://stackoverflow.com/questions/65831613/remove-tag-from-xml-file

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You should add the Namespace when you try to get the workbookProtection, by using Linq to Xml :

1 - Declare the namespace :

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

2 - Remove the workbookProtection

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

For Test :

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

Result:

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

I hope you find this helpful.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...