Read_XML_Sites_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_XML_Sites_Class
13| {
14| string sSites_Parameter_File = "";
15|
16| //declare an array of objects
17| public Sites_Class[] oSite;
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 int iXml_Browser = "";
25| public string sXml_Url = "";
26| public string sXml_Description = "";
27| public string sXml_Password = "";
28|
29| //constructor method
30| public Read_XML_Sites_Class(string sSites_XML_File_Location)
31| {
32| //Set the location of this user's Apps XML file
33| sSites_Parameter_File = sSites_XML_File_Location;
34| }
35|
36| //does xml file exit?
37| public bool Parameter_File_Exist()
38| {
39| bool bExists = true;
40|
41| if (!File.Exists(sSites_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(sSites_Parameter_File);
57| oNodeReader = new XmlNodeReader(oDoc);
58|
59| //Allocate objects in the User_Class
60| oApp = new Sites_Class[Sites_Class.iMaxSiteObjects];
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("browser1"))
100| {
101|
Sites_Class.sBrowser[0] = oNodeReader.Value;
102| }
103|
104| if (sElement.Equals("browser2"))
105| {
106|
Sites_Class.sBrowser[1] = oNodeReader.Value;
107| }
108|
109| if (sElement.Equals("browser"))
110| {
111|
iXml_Browser = int.Parse(oNodeReader.Value);
112| }
113|
114| if (sElement.Equals("url"))
115| {
116| sXml_Url = oNodeReader.Value;
117| }
118|
119| if (sElement.Equals("description"))
120| {
121| sXml_Description = oNodeReader.Value;
122| }
123|
124| if (sElement.Equals("password"))
125| {
126| sXml_Password = oNodeReader.Value;
127| }
128|
129| }//end of if
130|
131| //test if all content for user node has been read
132| if (XmlNodeType.EndElement == oNodeReader.NodeType)
133| {
134| if (oNodeReader.Name.Equals("site"))
135| {
136| Add_Object();
137| }
138| }
139|
140| }//end of while
141|
142| oNodeReader.Close();
143|
144| }//end of method
145|
146| //add an object to the user class
147| private void Add_Object()
148| {
149| //Is there room for another object?
150| //Can we instantiate more objects?
151| if (Sites_Class.iNextSiteObj < Sites_Class.iMaxSiteObjects)
152| {
153| oUser[Apps_Class.iNextAppObj] =
154| new Sites_Class(iXml_Browser, sXml_Url, sXml_Description, sXml_Password);
155|
156| //Clear out the holding area fields
157| iXml_Browser = 0;
158| sXml_Url = "";
159| sXml_Description = "";
160| sXml_Password = "";
161| }
162|
163| }
164| }//end of class
165|
166| }