wordpress hit counter
Re: Problem to update CustomXMLPart with feedData - WordprocessingML - Formats - OpenXML Developer

Re: Problem to update CustomXMLPart with feedData

Formats

Discussions about working with different Open XML Formats

Problem to update CustomXMLPart with feedData

  • rated by 0 users
  • This post has 2 Replies |
  • 1 Follower
  • Dear all,

     

    I have a document, with a customXLM defined, and binding made manually with a tool.

    I populate some part of the customXML with a call to a webservice, that's why I  would like to remplace some part of the CustomXML.

    This task is ok, I replace some XMLNode in a XMLDocument.

    But when I want to save the customXMLPart by calling FeedData, it's doesnt work. i insert some part of code, if someone has an idea, would be really appreciated.

     

    private void FeedCustomXMLPart(WordprocessingDocument wordDoc, XmlElement xmlElement)
            {        

             

                    if (wordDoc.MainDocumentPart == null)
                    {
                        throw new ArgumentNullException("mainDocumentPart missing in GlobalTemplate");
                    }
                    XmlDocument xmlDocument = new XmlDocument();
                    XmlNode objNode = null;
                    CustomXmlPart result = null;
                    foreach(CustomXmlPart part in wordDoc.MainDocumentPart.CustomXmlParts){
                         using (XmlTextReader reader = new XmlTextReader(part.GetStream(FileMode.Open, FileAccess.ReadWrite))){
                       
                                 reader.MoveToContent();
                                 bool exists = reader.NamespaceURI.Equals(TEMPLATE_CATEGORY);
                                 if (exists)
                                 {
                                     result = part;
                                   
                                     xmlDocument.Load(reader);
                                     XmlElement objRootXmlElement = xmlDocument.DocumentElement;
                                     //Create an XmlNamespaceManager for resolving namespaces
                                     XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDocument.NameTable);
                                     nsmgr.AddNamespace("ns0",TEMPLATE_CATEGORY);                            
                                     objNode = objRootXmlElement.SelectSingleNode("/ns0:root[1]/ns0:external[1]", nsmgr);                              
                                     if (objNode == null)
                                     {
                                         throw new ArgumentNullException("No XMlNode found for GlobalTemplate");
                                     }
                                     else
                                     {
                                         XmlDocumentFragment xmlDocFragment = xmlDocument.CreateDocumentFragment();
                                         string test = xmlElement.OuterXml;
                                         xmlDocFragment.InnerXml = xmlElement.OuterXml;
                               
                                         objNode.ReplaceChild(xmlDocFragment,objNode.FirstChild);
                                         Logger.TraceInfo(TraceLevelEnum.Information, CLASS_NAME + ".FeedCustomXMLPart", "FeedCustomXMLPart before replace " + objNode.OuterXml);
                                         using (MemoryStream xmlStream = new MemoryStream())
                                         {
                                             xmlDocument.Save(xmlStream);
                                             IngeniumLogger.TraceInfo(TraceLevelEnum.Information, CLASS_NAME + ".FeedCustomXMLPart", "XmlDocument " + xmlDocument.OuterXml);

                                             xmlStream.Seek(0, SeekOrigin.Begin);
                                             byte[] jsonbyte = new byte[xmlStream.Length];
                                             xmlStream.Read(jsonbyte, 0, (int)xmlStream.Length);
                                             Logger.TraceInfo(TraceLevelEnum.Information, CLASS_NAME + ".FeedCustomXMLPart", "MemoryStream " + Encoding.UTF8.GetString(jsonbyte));

                                             result.FeedData(xmlStream);
                                             wordDoc.Close();
                                   
                                           
                                         }

                                       

                                     }
                                 }
                            }
                       
                             
                    }
                         
            }

     

     

     

  • Hello eliselavy,

    I'm not sure if this will solve the specific problem with your content, but in the past when I've had trouble with stream output, it has helped to use the Flush method on the underlying package. You can access the package via the Package property inherited by the WordprocessingDocument class. You might want to call it before calling Close. (I know, it seems counterintuitive to have to call Flush when youre calling Close already.) I would be interested to know if that helps.

    Jim

  • thank you so much Jim, it works by calling the flush method:

     result.FeedData(xmlStream);

                               wordDocSource.Package.Flush();

                               wordDocSource.Close();

Page 1 of 1 (3 items)