none
How to Delete CustomXmlPart from .docx RRS feed

  • Question

  • Let's say I have 2 CustomXmlParts within a document. (So my .docx package contains item1.xml and item2.xml, both of different schema types)   I only want to delete one of them.  I've found a way to delete all CustomXmlParts, but how can I just delete the one I want to remove? 

     

     

    //This removes all pieces, but I only want to remove the one containing a certain root element

    using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(documentOrig, true))

    {

     

       MainDocumentPart mainPart = wordDoc.MainDocumentPart;

       if (mainPart.CustomXmlParts != null)

       {

          mainPart.DeleteParts(mainPart.CustomXmlParts);

       }

     

    }

     

    Tuesday, July 29, 2008 10:18 PM

Answers

  •  

    I am not sure of a straighforward way to do this but if you have the Id of the CustomXml Properties part (defined in the CustomXml rels file) you can try something like this:

     

    Code Snippet

    IEnumerable<CustomXmlPart> _customParts = _mPart.CustomXmlParts;

    foreach (CustomXmlPart _customPart in _customParts)

    {

    // Get the Id of the CustomXml Properties file

    string _partId = _mPart.GetIdOfPart(_customPart.CustomXmlPropertiesPart);

    if (_partId.Equals("PARTIDTODELETE"))

    _mPart.DeletePart(_customPart);

    }

     

     

     

     

     

     

     

     

     

    Wednesday, July 30, 2008 6:30 AM
  • Cool.  I really appreciate your replies. The way I found to do it follows:

     

    MainDocumentPart mainPart = wordDoc.MainDocumentPart;

    string searchString = "//someTongue Tiedearchpath";

    foreach (CustomXmlPart part in mainPart.CustomXmlParts) {

     

        XmlDocument customPropsDoc = new XmlDocument();

        customPropsDoc.Load(part.GetStream());

        XmlNode xn = customPropsDoc.SelectSingleNode(searchString, NamespaceManager);

         if (xn != null)

         {

             list.Add(part);

         }

     

    }

     

    foreach (CustomXmlPart del in list)

    {

        mainPart.DeletePart(del);

    }

    Wednesday, July 30, 2008 4:25 PM

All replies

  •  

    I am not sure of a straighforward way to do this but if you have the Id of the CustomXml Properties part (defined in the CustomXml rels file) you can try something like this:

     

    Code Snippet

    IEnumerable<CustomXmlPart> _customParts = _mPart.CustomXmlParts;

    foreach (CustomXmlPart _customPart in _customParts)

    {

    // Get the Id of the CustomXml Properties file

    string _partId = _mPart.GetIdOfPart(_customPart.CustomXmlPropertiesPart);

    if (_partId.Equals("PARTIDTODELETE"))

    _mPart.DeletePart(_customPart);

    }

     

     

     

     

     

     

     

     

     

    Wednesday, July 30, 2008 6:30 AM
  • Cool.  I really appreciate your replies. The way I found to do it follows:

     

    MainDocumentPart mainPart = wordDoc.MainDocumentPart;

    string searchString = "//someTongue Tiedearchpath";

    foreach (CustomXmlPart part in mainPart.CustomXmlParts) {

     

        XmlDocument customPropsDoc = new XmlDocument();

        customPropsDoc.Load(part.GetStream());

        XmlNode xn = customPropsDoc.SelectSingleNode(searchString, NamespaceManager);

         if (xn != null)

         {

             list.Add(part);

         }

     

    }

     

    foreach (CustomXmlPart del in list)

    {

        mainPart.DeletePart(del);

    }

    Wednesday, July 30, 2008 4:25 PM