Read_User_XML_Parameters_Class

    1| using System;
    2| using System.Collections.Generic;
    3| using System.Linq;
    4| using System.Text;
    5| 
    6| //add references
    7| using System.IO;
    8| using System.Xml;
    9| 
  10| namespace MyDesktop
  11| {
  12|     class Read_User_XML_Parameters_Class
  13|     {
  14|         const string sUser_Parameter_File = "MyDesktop_Users.xml";
  15| 
  16|         //declare an array of objects
  17|         public User_Class[] oUser;
  18| 
  19|         //Declare objects in required classes to read an xml file
  20|         XmlDocument oDoc;
  21|         XmlNodeReader oNodeReader;
  22| 
  23|         //Area to store xml user node content
  24|         public string sXml_Group = "";
  25|         public string sXml_UserID = "";
  26|         public string sXml_Drives = "";
  27|         public string sXml_Apps = "";
  28|         public string sXml_Sites = "";
  29| 
  30|         //constructor method
  31|         public Read_User_XML_Parameters_Class()
  32|         {
  33| 
  34|         }
  35| 
  36|         //does xml file exit?
  37|         public bool Parameter_File_Exist()
  38|         {
  39|             bool bExists = true;
  40| 
  41|             if (!File.Exists(sUser_Parameter_File))
  42|             {
  43|                 bExists = false;
  44|             }
  45|             return bExists;
  46|         }
  47| 
  48|         //open the xml file and declare an array of user objects
  49|         public bool Open_XML_Parameter_File()
  50|         {
  51|             bool bOpened = true;
  52| 
  53|             try
  54|             {
  55|                 oDoc = new XmlDocument();
  56|                 oDoc.Load(sUser_Parameter_File);
  57|                 oNodeReader = new XmlNodeReader(oDoc);
  58| 
  59|                 //Allocate objects in the User_Class
  60|                 oUser = new User_Class[User_Class.iMaxObjects];
  61|             }
  62|             catch
  63|             {
  64|                 bOpened = false;
  65|             }
  66| 
  67|             return bOpened;
  68|         }
  69| 
  70|         //read each xml user file
  71|         public void Read_User_Nodes()
  72|         {
  73|             string sElement = "";
  74| 
  75|             //Read the file
  76|             while (oNodeReader.Read())
  77|             {
  78|                 //bypass the prologue
  79|                 if (XmlNodeType.XmlDeclaration == oNodeReader.NodeType)
  80|                 {
  81|                     continue;
  82|                 }
  83| 
  84|                 //bypass any comment line
  85|                 if (XmlNodeType.Comment == oNodeReader.NodeType)
  86|                 {
  87|                     continue;
  88|                 }
  89| 
  90|                 //Identify and store element name
  91|                 if (XmlNodeType.Element == oNodeReader.NodeType)
  92|                 {
  93|                     sElement = oNodeReader.Name;
  94|                 }
  95| 
  96|                 //Process user node content
  97|                 if (XmlNodeType.Text == oNodeReader.NodeType)
  98|                 {
  99|                     if (sElement.Equals("user_id"))
100|                     {
101|                         sXml_UserID = oNodeReader.Value;
102|                     }
103| 
104|                     if (sElement.Equals("group"))
105|                     {
106|                         sXml_Group = oNodeReader.Value;
107|                     }
108| 
109|                     if (sElement.Equals("drives_parameters"))
110|                     {
111|                         sXml_Drives = oNodeReader.Value;
112|                     }
113| 
114|                     if (sElement.Equals("apps_parameters"))
115|                     {
116|                         sXml_Apps = oNodeReader.Value;
117|                     }
118| 
119|                     if (sElement.Equals("sites_parameters"))
120|                     {
121|                         sXml_Sites = oNodeReader.Value;
122|                     }
123| 
124|                 }//end of if
125| 
126|                 //test if all content for user node has been read
127|                 if (XmlNodeType.EndElement == oNodeReader.NodeType)
128|                 {
129|                     if (oNodeReader.Name.Equals("user"))
130|                     {
131|                         Add_Object();
132|                     }
133|                 }
134| 
135|             }//end of while
136| 
137|             oNodeReader.Close();
138| 
139|         }//end of method
140| 
141|         //add an object to the user class
142|         private void Add_Object()
143|         {
144|             string sType = "Group";
145| 
146|             if (sXml_Group.Equals(""))
147|             {
148|                 sType = "User";
149|             }
150| 
151|             //Is there room for another object?
152|             //Can we instantiate more objects?
153|             if (User_Class.iNextObj < User_Class.iMaxObjects)
154|             {
155|                 oUser[User_Class.iNextObj] =
156|                   new User_Class(sType, sXml_Group, sXml_UserID,
157|                                           sXml_Drives, sXml_Apps, sXml_Sites);
158| 
159|                 //Clear out the holding area fields
160|                 sXml_Group = "";
161|                 sXml_UserID = "";
162|                 sXml_Drives = "";
163|                 sXml_Apps = "";
164|                 sXml_Sites = "";
165|             }
166| 
167|         }
168|     }//end of class
169| }