ToXmlString() public method

public ToXmlString ( TreeNode node ) : string
node TreeNode
return string
        public async Task<TreeNode<NavigationNode>> GetTree()
        {
            // ultimately we will need to cache sitemap per site

            if (rootNode == null)
            {
                NavigationTreeXmlConverter converter = new NavigationTreeXmlConverter();

                await cache.ConnectAsync();
                byte[] bytes = await cache.GetAsync(cacheKey);
                if (bytes != null)
                {
                    string xml = Encoding.UTF8.GetString(bytes);
                    XDocument doc = XDocument.Parse(xml);
                    
                    rootNode = converter.FromXml(doc);
                }
                else
                {
                    rootNode = await BuildTree();
                    string xml2 = converter.ToXmlString(rootNode);

                    await cache.SetAsync(
                                        cacheKey,
                                        Encoding.UTF8.GetBytes(xml2),
                                        new DistributedCacheEntryOptions().SetSlidingExpiration(
                                            TimeSpan.FromSeconds(100))
                                            );
                                        }

                
            }

            return rootNode;
        }
        public async Task <TreeNode <NavigationNode> > GetTree()
        {
            // ultimately we will need to cache sitemap per site
            // we will implement a custom ICacheKeyResolver to resolve multi tenant cache keys

            if (rootNode == null)
            {
                log.LogDebug("rootnode was null so checking distributed cache");
                string cacheKey = cacheKeyResolver.ResolveCacheKey(options.CacheKey);

                NavigationTreeXmlConverter converter = new NavigationTreeXmlConverter();

                await cache.ConnectAsync();

                byte[] bytes = await cache.GetAsync(cacheKey);

                if (bytes != null)
                {
                    log.LogDebug("rootnode was found in distributed cache so deserializing");
                    string    xml = Encoding.UTF8.GetString(bytes);
                    XDocument doc = XDocument.Parse(xml);

                    rootNode = converter.FromXml(doc);
                }
                else
                {
                    log.LogDebug("rootnode was not in cache so building");

                    rootNode = await implementation.GetTree();

                    string xml2 = converter.ToXmlString(rootNode);

                    await cache.SetAsync(
                        cacheKey,
                        Encoding.UTF8.GetBytes(xml2),
                        new DistributedCacheEntryOptions().SetSlidingExpiration(
                            TimeSpan.FromSeconds(options.CacheDurationInSeconds))
                        );
                }
            }

            return(rootNode);
        }
        public async Task<TreeNode<NavigationNode>> GetTree()
        {
            // ultimately we will need to cache sitemap per site
            // we will implement a custom ICacheKeyResolver to resolve multi tenant cache keys

            if (rootNode == null)
            {
                log.LogDebug("rootnode was null so checking distributed cache");
                string cacheKey = cacheKeyResolver.ResolveCacheKey(options.CacheKey);

                NavigationTreeXmlConverter converter = new NavigationTreeXmlConverter();

                await cache.ConnectAsync();
                byte[] bytes = await cache.GetAsync(cacheKey);
                if (bytes != null)
                {
                    log.LogDebug("rootnode was found in distributed cache so deserializing"); 
                    string xml = Encoding.UTF8.GetString(bytes);
                    XDocument doc = XDocument.Parse(xml);

                    rootNode = converter.FromXml(doc);
                }
                else
                {
                    log.LogDebug("rootnode was not in cache so building");

                    rootNode = await implementation.GetTree();
                    string xml2 = converter.ToXmlString(rootNode);

                    await cache.SetAsync(
                                        cacheKey,
                                        Encoding.UTF8.GetBytes(xml2),
                                        new DistributedCacheEntryOptions().SetSlidingExpiration(
                                            TimeSpan.FromSeconds(options.CacheDurationInSeconds))
                                            );
                }


            }

            return rootNode;
        }
Esempio n. 4
0
        public async Task <TreeNode <NavigationNode> > GetTree()
        {
            // ultimately we will need to cache sitemap per site

            if (rootNode == null)
            {
                NavigationTreeXmlConverter converter = new NavigationTreeXmlConverter();

                await cache.ConnectAsync();

                byte[] bytes = await cache.GetAsync(cacheKey);

                if (bytes != null)
                {
                    string    xml = Encoding.UTF8.GetString(bytes);
                    XDocument doc = XDocument.Parse(xml);

                    rootNode = converter.FromXml(doc);
                }
                else
                {
                    rootNode = await BuildTree();

                    string xml2 = converter.ToXmlString(rootNode);

                    await cache.SetAsync(
                        cacheKey,
                        Encoding.UTF8.GetBytes(xml2),
                        new DistributedCacheEntryOptions().SetSlidingExpiration(
                            TimeSpan.FromSeconds(100))
                        );
                }
            }

            return(rootNode);
        }
        public async Task Can_Roundtrip_Serialize_To_Xml()
        {
            // Assemble
            HardCodedNavigationTreeBuilder builder = new HardCodedNavigationTreeBuilder();
            TreeNode<NavigationNode> rootNode = await builder.GetTree();
            NavigationTreeXmlConverter converter = new NavigationTreeXmlConverter();

            //Act
            string xml = converter.ToXmlString(rootNode);

            Assert.True(xml.Length > 10);

            //using (StreamWriter stream = File.CreateText("dumpxml.txt"))
            //{
            //    stream.WriteLine(xml);
            //}
        }