Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8310423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T19:27:19+00:00 2026-06-08T19:27:19+00:00

I am using OpenXML SDK ver 2 to manipulate some word documents. The documents

  • 0

I am using OpenXML SDK ver 2 to manipulate some word documents. The documents currently have custom xml parts and what I want to do is replace the xml for the parts specifically.

I know I can do something like this to access the customxml parts of a document:

Dim mainStream As New MemoryStream()
Dim buffer As Byte() = File.ReadAllBytes(Server.MapPath("myfile.docx"))
mainStream.Write(buffer, 0, buffer.Length)

Try
  Using mainDocument As WordprocessingDocument = WordprocessingDocument.Open(mainStream, True)

  MainDocumentPart mainPart = mainDocument.MainDocumentPart;
  'collection of custom xml parts
   Dim parts = mainPart.CustomXmlParts
   For Each part As CustomXmlPart In parts
     'how do I replace the xml here??
   Next

But as you can see I’m not sure how to replace the XML of the part. My document has two XML parts called item1.xml and item2.xml. I want to replace the XML in those parts. I know I can use .DeleteParts() to remove the existing xml parts and I can use AddCustomXmlPart() to create new parts but I don’t want to do that. I simply want to replace the XML for the existing parts.

Could anyone suggest how I could do this? Any advice is appreciated.

Thank you.

Edit oops forgot the code tags

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T19:27:23+00:00Added an answer on June 8, 2026 at 7:27 pm

    Use the FeedData() method of the CustomXmlPart instance to replace the
    XML of the custom xml part. The FeedData() method at first truncates
    the stream of the part, then writes the new data to the part stream.
    So, you could use this method to replace the XML in an existing custom xml part.

    MainDocumentPart mainPart = mainDocument.MainDocumentPart;
    
    Dim parts = mainPart.CustomXmlParts
    
    For Each part As CustomXmlPart In parts
    
      Dim ms As New MemoryStream
      Dim xtw As New XmlTextWriter(ms, Encoding.UTF8)
    
      ' Create your xml.'
    
      xtw.WriteStartDocument() 
      xtw.WriteStartElement("bla")
      xtw.WriteEndElement()
      xtw.WriteEndDocument()
      xtw.Flush()
    
      ms.Seek(0, SeekOrigin.Begin)
    
      part.FeedData(ms) ' Replace old xml in part stream.'
    
      xtw.Close()    
    
    Next
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a library that generates Word documents using the OpenXML SDK, one of
I am parsing some Openxml word documents using the .Net OpenXml SDK 2.0. I
I'm using the OpenXML SDK to programatically replace some <w:sdt/> elements with chunks of
I have a xml which I want it to be extracted using OpenXML within
Im using Open XML SDK to read and write information to custom xml parts
I am using Microsoft Open XML SDK to generate a word document. I have
I have created an Excel document using OpenXml SDK 2.0, now I have to
I'm trying to merge two docx-documents into one docx-document using OpenXML SDK 2.0. The
I'm searching for strings inside a word document using the Open XML Office SDK
I'm using version 1.0 of Microsoft's OpenXML SDK to do some basic parsing of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.