Open XML SDK: How to read Content_Types.xml file of PresentationDocument instance

AdamShakhabov 1 Reputation point
2021-09-10T18:14:04.413+00:00

Is there some API in Open XML SDK to read Content_Types.xml? I mean:

// 1. Open presentation  
PresentationDocument doc = PresentationDocument.Open(@"test.pptx", true);  
  
// 2. Update presentation  
// ...  
  
// 3. Read Content_Types.xml content  
// ???  
  
// 4. Close presentation  
doc.Close();  

I wanna access to the raw data of the Content_Types.xml file in fly — with no necessary to save the presentation in intermediate file/stream, then open this .zip by other libraries.

131212-image.png

I couldn't find an appropriate method of PresentationDocument instance or its child to get this data.

Developer technologies C#
{count} votes

2 answers

Sort by: Most helpful
  1. P a u l 10,761 Reputation points
    2021-09-10T18:47:47.867+00:00

    I'm not familiar with Open XML but I have noticed there's a "PartExtensionProvider" with the description:

    Gets a PartExtensionProvider part which provides a mapping from ContentType to part extension.  
    

    https://learn.microsoft.com/en-us/dotnet/api/documentformat.openxml.packaging.presentationdocument?view=openxml-2.8.1

    You can treat it like a Dictionary<string, string> where the key is the "ContentType" and "PartName" is the value:
    https://learn.microsoft.com/en-us/dotnet/api/documentformat.openxml.packaging.partextensionprovider?view=openxml-2.8.1

    Apologies if this isn't what you're after.


  2. Timon Yang-MSFT 9,606 Reputation points
    2021-09-13T06:16:52.227+00:00

    Such information is stored in XXXPart.ContentType Property.

    The following code can get a part of it, not a complete solution, but it should provide you with an idea.

            public static void CreatePresentation(string filepath)  
            {  
                // Create a presentation at a specified file path. The presentation document type is pptx, by default.  
                PresentationDocument presentationDoc = PresentationDocument.Open(filepath,false);  
                PresentationPart presentationPart = presentationDoc.PresentationPart;  
                foreach (var item in presentationDoc.Parts)  
                {  
                    Console.WriteLine(item.OpenXmlPart.ContentType);  
                }  
                Console.WriteLine("~~~~~~~~~~~~~~~~~~~~~~");  
                foreach (var slidePart in presentationPart.SlideParts)  
                {  
                   
                    Console.WriteLine(slidePart.ContentType);  
                }  
                presentationDoc.Close();  
            }  
    

    I have another idea, whether we can use the code to decompress it, then use the conventional method of reading Xml to read the content, and finally delete the decompressed file, I think this might be easier.

    ZipFile.ExtractToDirectory Method


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.