none
OpenXML SDK 2.0 snippet problem - XLInsertCustomXML RRS feed

  • Question

  • very much appreciate any help and guidance anyone can lend.

    i'm trying to use the OpenXML SDK 2.0's XLInsertCustomXml snippet in a C# VS2008 project.  Upon adding the snippet, I receive one error associated with "wbPart.AddNewPart<CustomXmlPart>()" which reads:

    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'.

    Does anyone know why this SDK snippet is dysfunctional and how to correct the error?

    Thanks!

    Monday, August 9, 2010 10:54 AM

Answers

  • Are you trying to add CustomXML to a document, if that's so there has been a change in how you do it I think.

     

    Instead of using AddNewPart you use AddCustomXmlPart(XElement custompart). Here is a code example:

     

    public void insertXml(XElement customXml)
        {
          using (document = WordprocessingDocument.Open(outputFile, true))
          {
            CustomXmlPart xmlPart = document.MainDocumentPart.AddCustomXmlPart(CustomXmlPartType.CustomXml);
            using (Stream partStream = xmlPart.GetStream(FileMode.Create, FileAccess.Write))
            {
              using (StreamWriter outputStream = new StreamWriter(partStream))
              {
                outputStream.Write(customXml);
              }
            }
            document.MainDocumentPart.Document.Save();
         }
        }
    
    If that's not what you where looking for I'm sorry i couldn't help.

    Thursday, August 12, 2010 12:08 PM

All replies

  • Are you trying to add CustomXML to a document, if that's so there has been a change in how you do it I think.

     

    Instead of using AddNewPart you use AddCustomXmlPart(XElement custompart). Here is a code example:

     

    public void insertXml(XElement customXml)
        {
          using (document = WordprocessingDocument.Open(outputFile, true))
          {
            CustomXmlPart xmlPart = document.MainDocumentPart.AddCustomXmlPart(CustomXmlPartType.CustomXml);
            using (Stream partStream = xmlPart.GetStream(FileMode.Create, FileAccess.Write))
            {
              using (StreamWriter outputStream = new StreamWriter(partStream))
              {
                outputStream.Write(customXml);
              }
            }
            document.MainDocumentPart.Document.Save();
         }
        }
    
    If that's not what you where looking for I'm sorry i couldn't help.

    Thursday, August 12, 2010 12:08 PM
  • It would be nice if the on-line documentation was changed to reflect this -- took me many hours to find this ...

     

    Tuesday, October 5, 2010 11:18 AM
  • Does anyone know of an acutal working sample? I only see snippets and the sample that I found does not seem to work.

    Thanks,
    Don Rule

    Thursday, November 4, 2010 5:26 PM