Daan
Daan

Reputation: 2898

How can I delete an XMLSchemaReference when using WordProcessingDocument?

I want to delete an XMLSchemaReference from a Word document. When running VBA code, this is simple:

ActiveDocument.XMLSchemaReferences("ActionsPane3").Delete

When using the ThisDocument class in a VSTO, this is also simple with C#:

Globals.ThisDocument.XMLSchemaReferences["ActionsPane3"].Delete();

However, when using a instance of WordProcessingDocument (in a normal windows application), I do not know how to perform the same action. Any idea of how I should write my C# code?

Upvotes: 3

Views: 864

Answers (1)

Alexander Derck
Alexander Derck

Reputation: 14498

For problems like this, the best thing you can do is download the Open XML SDK productivity tool and compare the documents you made before and after you've made the change. When I use VSTO to add an actionspane and I explore the package in the tool I notice this:

enter image description here

Then I remove the actionspane with the code you gave:

Globals.ThisDocument.XMLSchemaReferences["ActionsPane3"].Delete();
this.Save();

If we look at the package in the tool now we have the following (please do notice the awesome free-hand writing):

enter image description here

Now that we have identified what needs to be removed we can start working with the open xml sdk (using DocumentFormat.OpenXml.Packaging to open the file and using DocumentFormat.OpenXml.Wordprocessing to modify it). It's always convenient to keep the document open in the tool to be able to use the tree structure to build up the code. First I write the code to open and save the document:

byte[] byteArray = File.ReadAllBytes(@"C:\WorkSpace\test\WordTest.docx");

using (var stream = new MemoryStream())
{
   stream.Write(byteArray, 0, byteArray.Length);
   using (WordprocessingDocument doc = WordprocessingDocument.Open(stream, true))
   {
       //Logic here
   }

   using (FileStream fs = new FileStream(@"C:\WorkSpace\test\WordTest_modified.docx", 
          FileMode.Create))
   {
      stream.WriteTo(fs);
   }
}

To remove the AttachedSchema you need the following code:

doc.MainDocumentPart.DocumentSettingsPart
                    .Settings
                    .GetFirstChild<AttachedSchema>()
                    .Remove();

As you see it's very convenient to write this with the tree structure next to you. To remove the SchemaReference you will need this code:

doc.MainDocumentPart.CustomXmlParts.First()
                    .CustomXmlPropertiesPart
                    .DataStoreItem
                    .SchemaReferences
                    .FirstChild
                    .Remove();

And there you go, the same as if you would have removed it in the VSTO application.

Edit: If I execute the following line to remove all the /docProps/custom.xml the action pane is gone:

doc.CustomFilePropertiesPart.Properties.RemoveAllChildren();

I can't really test if that's the expected behavior for you because I use a test document (no notable change in size), but seeing now my action pane is gone, it might be what you're looking for (the properties contain a reference to my local vsto file). I wish microsoft would document this kind of stuff a little better.

Upvotes: 1

Related Questions