locked
The type 'DocumentFormat.OpenXml.Packaging.CustomXmlPart' cannot be used as type parameter 'T' RRS feed

  • Question

  • Hi,


    Error    1    The type 'DocumentFormat.OpenXml.Packaging.CustomXmlPart' cannot be used as type parameter 'T' in the generic type or method 'DocumentFormat.OpenXml.Packaging.OpenXmlPartContainer.AddNewPart<T>()'. There is no implicit reference conversion from 'DocumentFormat.OpenXml.Packaging.CustomXmlPart' to 'DocumentFormat.OpenXml.Packaging.IFixedContentTypePart'.

    While using the DocumentFormat.OpenXML.dll (version 2.0.4725.0) I get this error for the following line of code:

    CustomXmlPart customXmlPart = mainPart.AddNewPart<CustomXmlPart>();

    The code compile fine with the DocumentFormat.OpenXML.dll (version 2.0.3215.0)

    Does anyone know what i've been missing?

    Thank you.

    Saturday, January 16, 2010 4:20 AM

Answers

  • Hi de5a1n7,

    Thanks for your question.

    Firstly I suggest you downloading the latest December CTP from: http://www.microsoft.com/downloads/details.aspx?FamilyID=C6E744E5-36E9-45F5-8D8C-331DF206E0D0&displaylang=en , there are some changes compared with previous versions.

    As to DocumentFormat.OpenXml.Packaging.OpenXmlPartContainer.AddNewPart<T>(), the type T should be both IFixedContentTypePart and OpenXmlPart. CustomXmlPart is not IFixedContentTypePart (There are different types such as AdditionalCharacteristics, Bibliography, CustomXml and InkContent), so you could not call method  OpenXmlPartContainer.AddNewPart<T>() without specifying any parameters. Instead, you could use OpenXmlPartContainer.AddNewPart<T> (String, String):

    public virtual T AddNewPart<T> (
    	string contentType,
    	string id
    ) where T : OpenXmlPart

    If you just want to add CustomXmlPart, you could also use four overloaded methods AddCustomXmlPart() of MainDocumentPart. All of these methods should specify the CustomXmlPartType.

    Hope this will help you. If you have any questions, please let me know.

    Thanks,

    Lu
    • Marked as answer by de5a1n7 Tuesday, January 19, 2010 7:01 AM
    Monday, January 18, 2010 9:24 AM

All replies

  • Hi de5a1n7,

    Thanks for your question.

    Firstly I suggest you downloading the latest December CTP from: http://www.microsoft.com/downloads/details.aspx?FamilyID=C6E744E5-36E9-45F5-8D8C-331DF206E0D0&displaylang=en , there are some changes compared with previous versions.

    As to DocumentFormat.OpenXml.Packaging.OpenXmlPartContainer.AddNewPart<T>(), the type T should be both IFixedContentTypePart and OpenXmlPart. CustomXmlPart is not IFixedContentTypePart (There are different types such as AdditionalCharacteristics, Bibliography, CustomXml and InkContent), so you could not call method  OpenXmlPartContainer.AddNewPart<T>() without specifying any parameters. Instead, you could use OpenXmlPartContainer.AddNewPart<T> (String, String):

    public virtual T AddNewPart<T> (
    	string contentType,
    	string id
    ) where T : OpenXmlPart

    If you just want to add CustomXmlPart, you could also use four overloaded methods AddCustomXmlPart() of MainDocumentPart. All of these methods should specify the CustomXmlPartType.

    Hope this will help you. If you have any questions, please let me know.

    Thanks,

    Lu
    • Marked as answer by de5a1n7 Tuesday, January 19, 2010 7:01 AM
    Monday, January 18, 2010 9:24 AM
  • If you just want to add CustomXmlPart, you could also use four overloaded methods AddCustomXmlPart() of MainDocumentPart

    Thanks Lu, this works just fine.

    I guess the example code that i found from msdn is using SDK 1.
    So in SDK2 we can just use AddCustomXMLPart() of the MainDocumentPart.
    Tuesday, January 19, 2010 7:01 AM
  • Hi de5a1n7,

    There may be some some sample code in MSDN that is still using SDK1, which we will update soon.

    Thanks,

    Lu

    Tuesday, January 19, 2010 9:13 AM
  • Hi Lu

    I also get the error, "The type 'DocumentFormat.OpenXml.Packaging.CustomXmlPart' cannot be used as type parameter 'T' in the generic type or method ", in the following code (which is from sample code off the web) ...

            var mainPart = wpd.MainDocumentPart;
            var xmlPart = mainPart.AddNewPart<CustomXmlPart>();

    I am using "Open XML SDK 2.0 December 2009 CTP for Microsoft Office".

    Your explanation...

    "If you just want to add CustomXmlPart, you could also use four overloaded methods AddCustomXmlPart() of MainDocumentPart. All of these methods should specify the CustomXmlPartType."

    ...is unfortunately not sufficient for my level of experience.

    Could you please clarify by implimenting your suggested changes to my above code?

    Thanks in advance
    Siggy

    Friday, March 19, 2010 2:53 AM
  • Hi Siggy,

    Thanks for your question.

    I will give you some explanation together with examples.

    If you look up the methods of MainDocumentPart, you will see four overloaded methods of AddCustomXmlPart, which are:

    public CustomXmlPart AddCustomXmlPart (string contentType)
    public CustomXmlPart AddCustomXmlPart (CustomXmlPartType partType)
    public CustomXmlPart AddCustomXmlPart (string contentType , string id)
    public CustomXmlPart AddCustomXmlPart (CustomXmlPartType partType , string id)

    You could see from the above APIs that all of them need to specify a CutomXmlPartType whether it is in the format of string or CustomXmlPartType class. The CustomXmlPartType is an enum which could be AdditionalCharacteristics, Bibliography, CustomXml or InkContent (you could refer to the APIs for detailed information). So you need to call the method like this (take the first API as an example):

    MainDocumentPart.AddCustomXmlPart(CustomXmlPartType.CustomXml);

    Similarly, if you use AddNewPart<T>(), you should call this method:

    public override T AddNewPart<T> (string contentType , string id) --> example: MainDocumentPart.AddNewPart<CustomXmlPart>("application/xml", "rId1");

    rather than the other two, in which you should input contentType as the first parameter.

    Regarding why you need to specify the contentType, please refer to my last post.

    Hope this helps. If you have any question, please let me know.

    Thanks,

    Lu

     

    • Proposed as answer by Siggy01 Wednesday, March 24, 2010 11:12 PM
    Friday, March 19, 2010 8:03 AM
  • Hi Lu

    I am still confused...

    In the sample code that I am trying to understand the "AddNewPart" method is used and this is the method that generates an error.

    Your response seems to ignore  the "AddNewPart" method and relates to the "AddCustomXmlPart" method.  Are you implying that somehow the "AddNewPart" method is no longer supported?

    (Aside:  I am frustrated that somehow my settings cause my IE browser session of the MSDN library to only display VB code when I am trying to view C# code.  Is there a setting that I need to change in order to view both VB and C# examples?)

    Siggy

    Tuesday, March 23, 2010 12:39 AM
  • Hi Lu - about my aside re the C# not being displayed - blush! I have now spotted the language filter that I forgot about!!!

    Siggy

    Tuesday, March 23, 2010 12:54 AM
  • Hi Siggy,

    Thanks for your reply.

    In Open XML SDK 2.0, both AddNewPart and AddCustomXmlPart methods could be used to add a new CustomXml part into a document, while the former is more general which could be used to add other parts as well and the latter could only be used to add a CustomXml part. Since CustomXml part is special (whose root element is not fixed, not a generic type which can be seen from your error info) to other parts, if you use method AddNewPart, you should call the one in my last post rather than the other two to specify the content type. Our suggestion is using AddCustomXmlPart in which you don't need to manually assign an id if you don't have to.

    Hope this helps. If you have any question, please let me know.

    Thanks,

    Lu

    Tuesday, March 23, 2010 1:22 AM
  • Hi Lu

    Thanks for your patience and responses.

    I think I am finally starting to understand my confusion in reading the MSDN library and assuming that all samples provided are bug free.

    The following link -
    "http://msdn.microsoft.com/en-us/library/documentformat.openxml.packaging.openxmlpartcontainer.addnewpart(office.14).aspx"

    defines 3 overloads...

    Overload List:
    OpenXmlPartContainer.AddNewPart ()  Add a new part of type T 
    OpenXmlPartContainer.AddNewPart (String)  Add a new part of type T 
    OpenXmlPartContainer.AddNewPart (String, String)  Add a new part of type T 

    The first 2 overloads require type T to be OpenXmlPart and IFixedContentTypePart.

    The sample code at

    "http://msdn.microsoft.com/en-us/library/bb456489.aspx"

    implements the first overload without parameters "mainPart.AddNewPart<CustomXmlPart>();".

    Excerpt from MSDN library:
    --------------------------

    // How to add a new document part to a package.
    public static void AddNewPart(string document, string fileName)
    {
       using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(document, true))
       {
          MainDocumentPart mainPart = wordDoc.MainDocumentPart;
                   
          CustomXmlPart myXmlPart = mainPart.AddNewPart<CustomXmlPart>();

          using (FileStream stream = new FileStream(fileName, FileMode.Open))
          {
             myXmlPart.FeedData(stream);
          }
        }
    }

    Excerpt end:-----------

    However, when I download this sample code I get the error mentioned earlier.

    I assume that this is because (as you mentioned earlier) <CustomXmlPart> is not IFixedContentTypePart and hence the 1st overload is NOT applicable.

    Your solution then is to use the 3rd overload.

    My conclusion then is that the sample code is incorrect.

    Thanks
    Siggy

    • Proposed as answer by Siggy01 Wednesday, March 24, 2010 11:12 PM
    Wednesday, March 24, 2010 11:12 PM
  • Hi Siggy,

    Thanks for your feedback.

    I think the sample code you are referring to is in V1, while the dll you are using is Open XML SDK 2.0. There are differences between the V1 and V2. The V2 SDK provides strongly-typed classes to manipulate documents that adhere to the Office Open XML File Formats Specification. That's why you get the error information. The sample code to add a new CustomXml part in V2 is here. You could also find other sample code in V2 from: http://msdn.microsoft.com/en-us/library/bb491088(office.14).aspx.

    Hope this helps. If you have any question, please let me know.

    Thanks,

    Lu

    Thursday, March 25, 2010 1:33 AM
  • Hi Lu

    Thanks again for your response.

    I am glad that I had included the hyper-link in my last post as I was convinced that I was looking at an SDK 2 sample code.  You are quite correct the code was in fact for version 1 and the version 2 code reflected the appropriate method and hence there is no "bug" in the MSDN sample code as I suggested.

    Siggy

    Thursday, March 25, 2010 8:32 PM