Advertisement
Sonic3R

DataLayer

Oct 21st, 2011
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.89 KB | None | 0 0
  1.  
  2.    public class UrlStorage
  3.    {
  4.       /// <summary>
  5.       /// Message string
  6.       /// </summary>
  7.       public static string messages;
  8.  
  9.       /// <summary>
  10.       /// Path to XML File
  11.       /// </summary>
  12.       public const string FILEXML = @"D:\somewhere\example.xml";
  13.      
  14.      
  15.      
  16.       /// <summary>
  17.       /// private constructor of UrlStorage
  18.       /// useful to avoid creating instance of it
  19.       /// </summary>
  20.       private UrlStorage() {
  21.       }
  22.  
  23.       /// <summary>
  24.       /// singleton pattern
  25.       /// </summary>
  26.       public static readonly UrlStorage instance = new UrlStorage();
  27.  
  28.       /// <summary>
  29.       /// read XML file
  30.       /// </summary>
  31.       public List<CategoriesCtrlDto> readXMLtoLIST()
  32.       {
  33.          List<CategoriesCtrlDto> CategoryList = null;
  34.  
  35.          try {
  36.            
  37.             if ( File.Exists( FILEXML ) ) {
  38.  
  39.                XmlDocument document = new XmlDocument();
  40.                              
  41.              
  42.                document.Load( FILEXML );
  43.  
  44.                XmlNodeList list_of_categories = document.SelectNodes( "/parent/Categories" );
  45.  
  46.                CategoryList = new List<CategoriesCtrlDto>();
  47.  
  48.              
  49.                foreach ( XmlNode nodecategory in list_of_categories ) {                
  50.  
  51.                          
  52.                   if ( nodecategory.HasChildNodes ) {
  53.  
  54.                      List<LinksCtrlDto> List1 = new List<LinksCtrlDto>();
  55.                      
  56.                      //if yes then we can create a list of links
  57.                      foreach ( XmlNode nodelinks in nodecategory.ChildNodes ) {
  58.                         List1.Add( new LinksCtrlDto( nodelinks.Attributes["url"].Value, nodelinks.Attributes["name"].Value, nodelinks.Attributes["descr"].Value ) );
  59.                      }
  60.  
  61.                      CategoriesCtrlDto categ = new CategoriesCtrlDto( nodecategory.Attributes["Name"].Value, List1 );
  62.                      CategoryList.Add( categ );
  63.                   }
  64.                }
  65.             } else {
  66.                //if file not exists, return an empty list of categories
  67.                CategoryList = new List<CategoriesCtrlDto>( 0 );
  68.             }
  69.             //we close the document
  70.             //document.Save();
  71.          } catch ( Exception e ) {
  72.             //show exception message
  73.             messages = e.Message;
  74.          }
  75.  
  76.          return CategoryList;
  77.       }
  78.  
  79.  
  80.       /// <summary>
  81.       /// Find a category name
  82.       /// it is useful if you want to prevent adding duplicate category
  83.       /// or check if the category name exists for remove process
  84.       /// </summary>
  85.       /// <param name="source"></param>
  86.       /// <param name="search"></param>
  87.       /// <param name="categ_name"></param>
  88.       /// <param name="mode"></param>
  89.       /// <returns></returns>
  90.       private bool find_category(string source, string search) {
  91.          bool result = false;        
  92.          XmlDocument document = new XmlDocument();
  93.  
  94.          try {
  95.             if ( File.Exists( source ) ) {
  96.                document.Load( source );
  97.  
  98.                foreach ( XmlNode category_nodes in document ) {
  99.                   if ( ( category_nodes.Attributes["Name"].Value == search ) ) {
  100.                      result = true;
  101.                      break;
  102.                   }
  103.                }
  104.             }
  105.          } catch {
  106.             throw;
  107.          } finally {
  108.             document.Save( source );
  109.          }
  110.          return result;
  111.       }
  112.  
  113.  
  114.  
  115.       /// <summary>
  116.       /// Find a category name
  117.       /// it is useful if you want to prevent adding duplicate category
  118.       /// or check if the category name exists for remove process
  119.       /// </summary>
  120.       /// <param name="source"></param>
  121.       /// <param name="search"></param>
  122.       /// <param name="categ_name"></param>
  123.       /// <param name="mode"></param>
  124.       /// <returns></returns>
  125.       private bool find_link( string source, string search, string categ_name ) {
  126.          bool result = false;
  127.          XmlDocument document = new XmlDocument();
  128.  
  129.          try {
  130.             if ( File.Exists( source ) ) {
  131.                document.Load( source );
  132.  
  133.                foreach ( XmlNode categ_node in document ) {
  134.                   XmlNodeList xnlist = document.SelectNodes( "/parent/Categories[@Name='" + categ_name + "']" );
  135.                   foreach ( XmlNode link_nodes in categ_node ) {
  136.                      if ( ( link_nodes.Attributes["url"].Value == search ) )
  137.                         result = true;
  138.                      break;
  139.                   }
  140.                }              
  141.             } else
  142.                result = false;
  143.          } catch {
  144.             throw;
  145.          } finally {
  146.             document.Save( source );
  147.          }
  148.          return result;
  149.       }
  150.  
  151.  
  152.       /// <summary>
  153.       /// add link to source file
  154.       /// to a specified category
  155.       /// </summary>
  156.       /// <param name="source"></param>
  157.       /// <param name="categoryname"></param>
  158.       /// <param name="links"></param>
  159.       /// <returns></returns>
  160.       public bool add_link( string category_name, string url, string name, string description ) {
  161.          bool result = false;
  162.  
  163.          XmlDocument document = new XmlDocument();
  164.          XmlTextWriter writer = new XmlTextWriter( FILEXML, Encoding.UTF8 );
  165.  
  166.          if ( string.IsNullOrEmpty( category_name ) )
  167.             throw new ArgumentNullException( "Add link to category name DL" );
  168.          if ( string.IsNullOrEmpty( url ) )
  169.             throw new ArgumentNullException( "Add link url name DL" );
  170.          if ( string.IsNullOrEmpty( name ) )
  171.             throw new ArgumentNullException( "Add link name DL" );
  172.          if ( string.IsNullOrEmpty( description ) )
  173.             throw new ArgumentNullException( "Add link description DL" );
  174.  
  175.          try {
  176.             if ( File.Exists( FILEXML ) ) {
  177.                document.Load( FILEXML );
  178.                document.PreserveWhitespace = false;
  179.  
  180.                XmlNodeList xnlist = document.SelectNodes( "/parent/Categories[@Name='" + category_name + "']" );
  181.  
  182.                if ( xnlist.Count > 0 ) {
  183.  
  184.                   //if there are no duplicate then we can add link in XML file
  185.                   if ( !find_link( FILEXML, url, category_name ) ) {
  186.                      XmlElement element = document.CreateElement( "link" );
  187.                      element.SetAttribute( "url", url );
  188.                      element.SetAttribute( "name", name );
  189.                      element.SetAttribute( "descr", description );
  190.                      xnlist[0].AppendChild( element );
  191.                      result = true;
  192.                   } else {
  193.                      result = false;
  194.                   }
  195.  
  196.                } else
  197.                   throw new ArgumentNullException( "xnlist empty DataLayer" );
  198.             }
  199.             if ( result ) {
  200.                document.WriteTo( writer );
  201.             }
  202.  
  203.          } catch {
  204.             throw;
  205.          } finally {
  206.             document.Save( FILEXML );
  207.          }
  208.      
  209.          return result;
  210.       }
  211.  
  212.       /// <summary>
  213.       /// Add a category into XML File
  214.       /// First, we search it for duplicates
  215.       /// </summary>
  216.       /// <param name="source"></param>
  217.       /// <param name="category"></param>
  218.       /// <returns></returns>
  219.       public bool add_category( string category_name ) {
  220.          bool result = false;
  221.  
  222.          XmlDocument document = null;
  223.          XmlTextWriter writer = null;
  224.  
  225.          if ( string.IsNullOrEmpty(category_name) )
  226.             throw new ArgumentNullException( "categories" );
  227.  
  228.          try {
  229.             if ( File.Exists( FILEXML ) ) {
  230.  
  231.                document = new XmlDocument();
  232.                writer = new XmlTextWriter( FILEXML, Encoding.UTF8 );
  233.  
  234.                document.Load( FILEXML );
  235.                   if ( !find_category( FILEXML, category_name ) ) {
  236.                      XmlElement element = document.CreateElement( "Categories" );
  237.                      element.SetAttribute( "Name", category_name );
  238.                      result = true;
  239.                   }    
  240.             } else
  241.                result = false;
  242.  
  243.             //if is all ok then write data to XML file
  244.             if ( result ) {
  245.                document.WriteTo( writer );
  246.             }
  247.          } catch {
  248.             throw;
  249.          } finally {
  250.             document.Save( FILEXML );
  251.          }
  252.          return result;
  253.       }
  254.  
  255.       /// <summary>
  256.       /// delete link if it has been found
  257.       /// </summary>
  258.       /// <param name="source"></param>
  259.       /// <param name="category_name"></param>
  260.       /// <param name="url"></param>
  261.       /// <returns></returns>
  262.       public bool delete_link( string category_name, string link_url ) {
  263.          bool result = false;
  264.  
  265.          XmlDocument document = new XmlDocument();
  266.        
  267.          if ( string.IsNullOrEmpty(category_name) )
  268.             throw new ArgumentNullException( "delete_link categories" );
  269.          if ( string.IsNullOrEmpty( link_url ) )
  270.             throw new ArgumentNullException( "delete_link DL" );
  271.  
  272.          try {
  273.             if ( File.Exists( FILEXML ) ) {
  274.                document.Load( FILEXML );
  275.  
  276.                if ( find_link( FILEXML, link_url, category_name ) ) {
  277.                   XmlNode xnlist = document.SelectSingleNode( "/parent/Categories[@Name='" + category_name + "'/link[@url='" + link_url + "']" );
  278.                   if ( xnlist != null ) {
  279.                      xnlist.ParentNode.RemoveChild( xnlist );
  280.                      result = true;
  281.                   }
  282.                }
  283.             }
  284.          } catch {
  285.             throw;
  286.          } finally {
  287.             document.Save( FILEXML );
  288.          }
  289.  
  290.          return result;
  291.       }
  292.  
  293.  
  294.       /// <summary>
  295.       /// delete category if it has been found
  296.       /// </summary>
  297.       /// <param name="source"></param>
  298.       /// <param name="category_name"></param>
  299.       /// <param name="name"></param>
  300.       /// <returns></returns>
  301.       public bool delete_category( string category_name ) {
  302.          bool result = false;
  303.  
  304.          XmlDocument document = new XmlDocument();
  305.  
  306.          if ( string.IsNullOrEmpty(category_name) )
  307.             throw new ArgumentNullException("delete_category DL");
  308.  
  309.             try {
  310.                if ( File.Exists( FILEXML ) ) {
  311.                   document.Load( FILEXML );
  312.                
  313.                      if ( find_category( FILEXML, category_name ) ) {
  314.                         XmlNode xnlist = document.SelectSingleNode( "/parent/Categories[@Name='" + category_name + "']" );
  315.                         if ( xnlist != null ) {
  316.                            xnlist.ParentNode.RemoveChild( xnlist );
  317.                            result = true;
  318.                         }  
  319.                   }
  320.                }
  321.             } catch {
  322.                throw;
  323.             } finally {
  324.                document.Save( FILEXML );
  325.             }
  326.          
  327.          return result;
  328.       }
  329.    }
  330.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement