none
Can't Update a Node's Attribute value RRS feed

  • Question

  • I'm having trouble successfully updating a simple Attribute's value for a selected XmlNode within an OpenXml document (Presentation, SlidePart).  Here's a partial code.  I get no errors, but the value of the attribute is not updated.

    Here is Xml from the slide.xml file:

    <a:solidFill>
      <a:srgbClr val="92D050" />
    </a:solidFill>
    

    Here is code in C# I'm using to try to update this node:

    xmldoc.Load(slidePart.GetStream());
    
    XmlNodeList xNodes = xmldoc.SelectNodes("//p:spTree/p:sp/p:spPr/a:solidFill/a:srgbClr", nsManager);
    
    foreach (XmlElement xNode in xNodes)
    {
      xNode.SetAttribute("val", NewValue);
      xmldoc.Save(slidePart.GetStream());
    }
    

    Yes, I am able to confirm I have the correct node and I can retrieve the text value for the "val" attribute.

    What is going on?  For this file I have previously updated InnerText values for other elements, and added new Image parts, etc., performing an XmlDocument.Save call after each of these udpates.  All of this is inside a Using statement for the overall Package and it is opened with the ReadWrite enumeration.  The above attempt to update the attribute value is done before closing the Package.  So why is it not saving the change?

    Thanks.

    Tuesday, March 1, 2011 4:51 PM

Answers

  • Appears the problem was that I had a prior SlidePart update performed prior to this xml update, and I think I did not properly save the SlidePart update.  I guess for some reason this interefered with properly saving the xml update.  This is odd though, considering that the SlidePart update did appear correctly in the final .pptx output and there were not errors/corruptions, but just the next node update did not save.
    • Marked as answer by sdfsda Wednesday, March 9, 2011 4:20 PM
    Wednesday, March 9, 2011 4:18 PM

All replies

  • Hi Sdfsda,

    Thank you for posting and we are glad to help with you.

    Could you show us some steps about creating the PowerPoint file? I tried create a PowerPoint file and insert some text, but I couldn't get the property a:srgbClr in p:sp.

    And below is my code snippet for achieving your goal. I don't know whether it is ok for I don't have a proper PowerPoint file which is similar with yours to test: 

    You can test with your PowerPoint file to see whether it works for you.

    I hope it can help you and feel free to follow up after you have tried.

    Best Regards,

    using System;
    using System.IO.Packaging;
    using System.IO;
    using System.Xml;
    
    namespace OpenXmlUpDateNodeAttributeValDebug
    {
      class Program
      {
        static void Main(string[] args)
        {
          string fileName = AppDomain.CurrentDomain.BaseDirectory + "Test.pptx";
          string documentRelationshipType = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument";
          string presentationmlNamespace = "http://schemas.openxmlformats.org/presentationml/2006/main";
          string drawingmlNamespace = "http://schemas.openxmlformats.org/drawingml/2006/main";
          string relationshipNamespace = "http://schemas.openxmlformats.org/officeDocument/2006/relationships";
    
          PackagePart documentPart = null;
          Uri documentUri = null;
    
          using (Package pptPackage = Package.Open(fileName, FileMode.Open, FileAccess.ReadWrite))
          {
            foreach (PackageRelationship relationship in pptPackage.GetRelationshipsByType(documentRelationshipType))
            {
              documentUri = PackUriHelper.ResolvePartUri(
               new Uri("/", UriKind.Relative), relationship.TargetUri);
              documentPart = pptPackage.GetPart(documentUri);
    
              break;
            }
    
            // Manage namespaces to perform Xml 
            // XPath queries.
            NameTable nt = new NameTable();
            XmlNamespaceManager nsManager = new XmlNamespaceManager(nt);
            nsManager.AddNamespace("p", presentationmlNamespace);
            nsManager.AddNamespace("a", drawingmlNamespace);
            nsManager.AddNamespace("r", relationshipNamespace);
    
            // Iterate through the slides and 
            // extract the title string from each.
            XmlDocument xDoc = new XmlDocument(nt);
            xDoc.Load(documentPart.GetStream());
    
            XmlNodeList sheetNodes =
             xDoc.SelectNodes("//p:sldIdLst/p:sldId/p:spTree", nsManager);
            if (sheetNodes != null)
            {
    
              foreach (XmlElement xNode in sheetNodes)
              {
                xNode.SetAttribute("val", "92D050");
              }
            }
            xDoc.Save(documentPart.GetStream());
    
            //xmldoc.Load(slidePart.GetStream());
    
            //XmlNodeList xNodes = xmldoc.SelectNodes("//p:spTree/p:sp/p:spPr/a:solidFill/a:srgbClr", nsManager);
    
            //foreach (XmlElement xNode in xNodes)
            //{
            //  xNode.SetAttribute("val", NewValue);
            //  xmldoc.Save(slidePart.GetStream());
            //}
    
          }
        }
      }
    }
    
    

    Bruce Song [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Proposed as answer by Bruce Song Wednesday, March 9, 2011 2:05 AM
    Wednesday, March 2, 2011 3:33 AM
  • Hi Sdfsda,

    Have you resolved your issue yet, and does the suggestion help you? If you still have any concern on the problem, just feel free to follow up. I will try my best to help you.

    Best Regards,


    Bruce Song [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, March 9, 2011 2:07 AM
  • Appears the problem was that I had a prior SlidePart update performed prior to this xml update, and I think I did not properly save the SlidePart update.  I guess for some reason this interefered with properly saving the xml update.  This is odd though, considering that the SlidePart update did appear correctly in the final .pptx output and there were not errors/corruptions, but just the next node update did not save.
    • Marked as answer by sdfsda Wednesday, March 9, 2011 4:20 PM
    Wednesday, March 9, 2011 4:18 PM
  • Hi Sdfsda,

    Thank you for your feedback, this may also do help to the other community members. You are welcome.

    Best Regards,


    Bruce Song [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, March 10, 2011 7:04 AM