OpenXML replace specific customxml part of word document

孤街醉人 提交于 2019-12-01 22:41:33

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!