BVNetwork.NotFound.Core.CustomRedirects.RedirectsXmlParser.Load C# (CSharp) Method

Load() public method

Parses the xml file and reads all redirects.
public Load ( ) : BVNetwork.NotFound.Core.CustomRedirects.CustomRedirectCollection
return BVNetwork.NotFound.Core.CustomRedirects.CustomRedirectCollection
        public CustomRedirectCollection Load()
        {
            const string URLPATH = "/redirects/urls/url";

            CustomRedirectCollection redirects = new CustomRedirectCollection();

            // Parse all url nodes
            XmlNodeList nodes = _customRedirectsXmlFile.SelectNodes(URLPATH);
            foreach (XmlNode node in nodes)
            {
                // Each url new url can have several old values
                // we need to create a redirect object for each pair
                XmlNode newNode = node.SelectSingleNode(NEWURL);

                XmlNodeList oldNodes = node.SelectNodes(OLDURL);
                foreach (XmlNode oldNode in oldNodes)
                {
                    bool skipWildCardAppend = false;
                    XmlAttribute skipWildCardAttr = oldNode.Attributes[SKIPWILDCARD];
                    if (skipWildCardAttr != null)
                    {
                        // If value parsing fails, it will be false by default. We do
                        // not really care to check if it fails, as we cannot do anything
                        // about it (throwing an exception is not a good idea here)
                        bool.TryParse(skipWildCardAttr.Value, out skipWildCardAppend);
                    }

                    // Create new custom redirect nodes
                    CustomRedirect redirect = new CustomRedirect(oldNode.InnerText, newNode.InnerText, skipWildCardAppend);
                    redirects.Add(redirect);
                }
            }

            return redirects;
        }

Usage Example

 public FileUploadJsonResult ImportRedirects(HttpPostedFileBase xmlfile)
 {
     CheckAccess();
     // Read all redirects from xml file
     RedirectsXmlParser parser = new RedirectsXmlParser(xmlfile.InputStream);
     // Save all redirects from xml file
     CustomRedirectCollection redirects = parser.Load();
     string message;
     if (redirects != null || redirects.Count != 0)
     {
         CustomRedirectHandler.Current.SaveCustomRedirects(redirects);
         message = string.Format(LocalizationService.Current.GetString("/gadget/redirects/importsuccess"), redirects.Count);
     }
     else
     {
         message = LocalizationService.Current.GetString("/gadget/redirects/importnone");
     }
     return new FileUploadJsonResult { Data = new { message = message } };
 }