in order to work with the published document. The eventhandler gives me the published document-object and I would like to get the xml for the document, but I cannot figure out how to get it from neither the node-object nor the document-object.
Looking at the umbraco source, I figured this out: umbraco.presentation.UmbracoContext.Current.Server.ContentXml.Descendants().Single(t => t.Attribute("isDoc") != null && (int)t.Attribute("id") == sender.Id);
but that relies on the umbraco.config file, and this is not updated yet, when I try to get the xml.
Is it possible to get the xml directly from a document-object or maybe from the db ?
The Document object has a method called "ToXml" which you could use?
var docXml = sender.ToXml(new XmlDocument(), false);
The method requires an exsiting XmlDocument, (only used to create new the XmlNode). The second parameter is "Deep", which you can set to true if you want all published child nodes too.
thanks for your input. I couldn't get Jeroen's suggestion to work when publishing a new document (I guess it is because it has not been published to the xml-file yet), whereas Lee's worked fine.
I know you guys are some of the most active people on the forum, so maybe you have a suggestion for the problem lying behind why I am trying to get the xml.
The thing is that I am using Linq2Umbraco (in order to get strongtyped objects) on a fairly large site with approx. 25.000 nodes. I use a static instance of the datacontext and store the Linq2Umbraco-context in cache in order to speed things up. The cache is then cleared when a document/node is updated/created/deleted and then the Linq2Umbraco-datacontext is reloaded. Unfortunately it takes approximately 2,5 minutes for the Linq2Umbraco-context to load. On my site the users can update some of the content without giving them access to the umbraco backend (they can only update a few fields). But when a document is published it takes 2,5 minutes to load reload the datacontext as mentioned before. This is of cource quite a bad user experience.
I have tried a few strategies to solve this, but so far without much luck. I have tried updating the datacontext async, but it throws an exception, since Linq2Umbraco cannot handle async operations it seems. I then tried creating a webrequest which would call a url which would create a new datacontext and replace the old one (in order to emulate async operation). But this also fails because the collection is then modified when a page using the context is reloaded (as far as I can tell). Now I am trying to get the xml and then create a strongtyped object and add it to the datacontext without reloading it. I don't know if this will work, but that is my next move.
I guess you guys have build some quite large Umbraco-sites (maybe even with Linq2Umbraco). Do you have any suggestions on how to handle this scenario?
Unfortunately I don't use Linq2Umbraco anymore because of this problem. I really love strongtyped objects in Umbraco, but Linq2Umbraco already get's slow with 2000 nodes. I think the best way to improve performance is to use something else instead of Linq2Umbraco. Perhaps this topic can help: http://our.umbraco.org/forum/developers/api-questions/18933-Query-multiple-websites
Get the xml for a node or document
I have attached an event-handler on
Document.AfterPublish -= new Document.PublishEventHandler(Document_AfterPublish);
in order to work with the published document. The eventhandler gives me the published document-object and I would like to get the xml for the document, but I cannot figure out how to get it from neither the node-object nor the document-object.
Looking at the umbraco source, I figured this out: umbraco.presentation.UmbracoContext.Current.Server.ContentXml.Descendants().Single(t => t.Attribute("isDoc") != null && (int)t.Attribute("id") == sender.Id);
but that relies on the umbraco.config file, and this is not updated yet, when I try to get the xml.
Is it possible to get the xml directly from a document-object or maybe from the db ?
thanks
Thomas
You can find the xml in the cmsContentXml table in the database, but I don't know if that is only the published xml.
You can also get the xml with this code, but it's also only published content:
Jeroen
Hi Thomas,
The Document object has a method called "ToXml" which you could use?
The method requires an exsiting XmlDocument, (only used to create new the XmlNode). The second parameter is "Deep", which you can set to true if you want all published child nodes too.
Cheers, Lee.
Hi Jeroen and Lee,
thanks for your input. I couldn't get Jeroen's suggestion to work when publishing a new document (I guess it is because it has not been published to the xml-file yet), whereas Lee's worked fine.
I know you guys are some of the most active people on the forum, so maybe you have a suggestion for the problem lying behind why I am trying to get the xml.
The thing is that I am using Linq2Umbraco (in order to get strongtyped objects) on a fairly large site with approx. 25.000 nodes. I use a static instance of the datacontext and store the Linq2Umbraco-context in cache in order to speed things up. The cache is then cleared when a document/node is updated/created/deleted and then the Linq2Umbraco-datacontext is reloaded. Unfortunately it takes approximately 2,5 minutes for the Linq2Umbraco-context to load. On my site the users can update some of the content without giving them access to the umbraco backend (they can only update a few fields). But when a document is published it takes 2,5 minutes to load reload the datacontext as mentioned before. This is of cource quite a bad user experience.
I have tried a few strategies to solve this, but so far without much luck. I have tried updating the datacontext async, but it throws an exception, since Linq2Umbraco cannot handle async operations it seems. I then tried creating a webrequest which would call a url which would create a new datacontext and replace the old one (in order to emulate async operation). But this also fails because the collection is then modified when a page using the context is reloaded (as far as I can tell). Now I am trying to get the xml and then create a strongtyped object and add it to the datacontext without reloading it. I don't know if this will work, but that is my next move.
I guess you guys have build some quite large Umbraco-sites (maybe even with Linq2Umbraco). Do you have any suggestions on how to handle this scenario?
thanks
Thomas
Hi Thomas,
Unfortunately I don't use Linq2Umbraco anymore because of this problem. I really love strongtyped objects in Umbraco, but Linq2Umbraco already get's slow with 2000 nodes. I think the best way to improve performance is to use something else instead of Linq2Umbraco. Perhaps this topic can help: http://our.umbraco.org/forum/developers/api-questions/18933-Query-multiple-websites
Jeroen
is working on a reply...