5

Here is what I am doing in my method on component save.

  • I am getting the component content and fetching all the "a" tags modifying it and replacing it. The content is modified but getting the following error.
  • System.ArgumentException: Illegal characters in path. at System.IO.Path.GetFileName(String path) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share) at System.Xml.XmlDocument.Save(String filename)

                XmlDocument doc = component.Content.OwnerDocument;
                string compContent = doc.OuterXml;               
                if (component == null) return;                                      
                List<string> aTags = GetATags(compContent);
                if (aTags.Count.Equals(0)) return;
                //Modify the href attribute value 
                foreach (string aTag in aTags)
                {
                    string refaTag = new string(aTag.ToCharArray());
                    string newAtag = ESUtil.SetHrefTagAttribute("href", refaTag);
                    compContent = compContent.Replace(refaTag, newAtag);
                }
                Logger.WriteWarning("Final doc" +compContent , ErrorCode.GENERAL);
                component.Content.OwnerDocument.Save(compContent);
    
  • The Final doc has my modified content.
8
  • You shouldn't try saving the component's XML like that, you need to set Component.Content = XmlElement - where this would be the root element of a XmlDocument like your doc.
    – Nuno Linhares
    Sep 15 '14 at 10:08
  • I tried something like this. doc.Save(compContent); component.Content = doc.OwnerDocument.DocumentElement; component.Save(); . What wrong am I doing here?
    – Pari
    Sep 15 '14 at 10:46
  • I would expect that to work - except for component.Save() which is not needed - this will actually trigger the event to happen again and may go into a loop - don't call component.Save() in an Initiated phase, Tridion will do the save for you.
    – Nuno Linhares
    Sep 15 '14 at 11:00
  • I agree component.save() is not required.Removed it.But I get the same error. "System.ArgumentException: Illegal characters in path. at System.IO.Path.GetFileName(String path) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share) at System.Xml.XmlDocument.Save(String filename)"
    – Pari
    Sep 15 '14 at 11:10
  • Don't call doc.Save then - try doc.LoadXml(compContent)
    – Nuno Linhares
    Sep 15 '14 at 11:23
4

Your code is trying to save XML into the file with path equal to your XML. What you need to do is set the value of component.Content property. After, depending on your event system implementetion you need to either call component.Save(), or do nothing.

If you have subscribed to Component SaveEventArgs in inititated state and this is your component - do nothing, otherwise call Save()

2
  • Yes, I have subscribed to Component SaveEventArgs EventSystem.Subscribe<Component, SaveEventArgs>(MyMethod, EventPhases.Initiated);.
    – Pari
    Sep 15 '14 at 9:38
  • I have not tried what you mentioned.component.Content.LoadXml(compContent). I am guessing even that should work.Thank you.
    – Pari
    Sep 16 '14 at 2:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.