Question : How to read XML

I have created two separate applications on is a setup tool that an admin will use to create all the configuration information for the users application. This configuration information is going to be stored inside an XML file. The user application will then read the XML file and apply all settings with in that XML file.

Part of those setting is displaying map images of a building in thumbnail format. Now I am unsure which way will be the easier of the two ideas I have. My first idea is to have the application read a directory destination from the XML file and display the images in that directory in thumbnail format. Then the user can click on the thumbnail and the image will be displayed in full size in a second window. In the second window the user should be able to zoom and pan the image.

 However the problem with idea one is I also need the icon image to be displayed over top the map images representing specific security device like a locked door, intercom station, video camera, and so on. So I thought the user application could read the image file name from the XML file and display that and it would also have the icon image location stated there as well. Grant I would still have to tell it what directory the image was in but the second idea might be easier since I need the icon image as well.

The XML schema I have is something like this:



   
      C:\Projects\AV4.1\Imagesectory>
     
        Testing image.bmp
       
         
            icon idle.bmp
            250,175n>
         

         
            icon idle.bmp
            270,175n>
         

         
            icon idle.bmp
            230,175n>
         

       

     

   

 



The code I am using for my two forms is attached below.
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
Form 1
---------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Xml.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows;
using System.Security.Principal;
using System.Xml;

namespace AV4._1_ClientTool.Dialogs1.View
{
    public partial class Maps : Form
    {
        public Maps()
        {
            InitializeComponent();
        }

        //form global variables
        ImageList thumbs = new ImageList();
        Size ThumbSize = new Size(64, 64);
        PictureBox pictureBox1 = new PictureBox();

        #region Thumbnails

        private void Maps_Load(object sender, EventArgs e)
        {       
            thumbs.ImageSize = ThumbSize;
            thumbs.ColorDepth = ColorDepth.Depth32Bit;
            listView1.View = System.Windows.Forms.View.LargeIcon;
            listView1.LargeImageList = thumbs;
 
            string path = @"C:\Projects\AV4.1\Images";

            pictureBox1.Size = thumbs.ImageSize;
            pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
            System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(path);
            foreach (System.IO.FileInfo fi in di.GetFiles("*.jpg"))
            {
                Bitmap bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);
                pictureBox1.Image = Image.FromFile(fi.FullName);
                pictureBox1.DrawToBitmap(bmp, pictureBox1.ClientRectangle);
                thumbs.Images.Add(bmp);
                ListViewItem lvi = new ListViewItem(System.IO.Path.GetFileName(fi.Name), thumbs.Images.Count - 1);
                lvi.Tag = fi.Name;
                listView1.Items.Add(lvi);
            }
            foreach (System.IO.FileInfo fi in di.GetFiles("*.gif"))
            {
                Bitmap bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);
                pictureBox1.Image = Image.FromFile(fi.FullName);
                pictureBox1.DrawToBitmap(bmp, pictureBox1.ClientRectangle);
                thumbs.Images.Add(bmp);
                ListViewItem lvi = new ListViewItem(System.IO.Path.GetFileName(fi.Name), thumbs.Images.Count - 1);
                lvi.Tag = fi.Name;
                listView1.Items.Add(lvi);
            }
        }
        #endregion

        #region Full Image Popup
        
        private void listView1_DoubleClick(object sender, EventArgs e)
        {
            Point pt = listView1.PointToClient(Cursor.Position);
            ListViewItem lvi = listView1.GetItemAt(pt.X, pt.Y);
            Dialogs1.Display.Map1 frm = new Dialogs1.Display.Map1((string)lvi.Tag);
            
            if (lvi != null)
            {
                frm.Text = lvi.Text;
                frm.Show();
            }
        }

        #endregion

    }
}
-------------------------------------------------------------------
Form 2
-------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Xml.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows;
using System.Security.Principal;
using System.Xml;


namespace AV4._1_ClientTool.Dialogs1.Display
{
    public partial class Map1 : Form
    {
        private double ZOOMFACTOR = 1.25;	// = 25% smaller or larger
        private int MINMAX = 5;		// 5 times bigger or smaller than the ctrl

        public Map1(string targetFileName)
        {
            InitializeComponent();

            this.pictureBox1.MouseWheel += new MouseEventHandler(pictureBox1_MouseWheel);

            if (!string.IsNullOrEmpty(targetFileName))
            {
                ShowFile(targetFileName);
            }
        }


        public void ShowFile(string targetFileName)
        {
            pictureBox1.Controls.Clear();
            foreach (var item in BusinessTier.IconFactory.ImageSet)
            {
                if (item.Image != targetFileName) continue;

                pictureBox1.Image = Image.FromFile(string.Format(@"{0}\{1}", item.Path, item.Image));
                foreach (var icon in item.Icons)
                {
                    var tempIcon = Image.FromFile(icon.File);
                    var tempPicturebox = new PictureBox
                    {
                        Image = tempIcon,
                        Location = new Point(icon.CoordinateX, icon.CoordinateY),
                        Size = new Size(tempIcon.Width, tempIcon.Height),
                    };
                    pictureBox1.Controls.Add(tempPicturebox);

                }
                break;
            }
        }

        #region Zooming Methods
        /// 
        /// Make the PictureBox dimensions larger to effect the Zoom.
        /// 
        /// Maximum 5 times bigger
        private void ZoomIn()
        {
            if ((pictureBox1.Width < (MINMAX * panel1.Width)) &&
                (pictureBox1.Height < (MINMAX * panel1.Height)))
            {
                pictureBox1.Width = Convert.ToInt32(pictureBox1.Width * ZOOMFACTOR);
                pictureBox1.Height = Convert.ToInt32(pictureBox1.Height * ZOOMFACTOR);
                pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
            }
        }

        /// 
        /// Make the PictureBox dimensions smaller to effect the Zoom.
        /// 
        /// Minimum 5 times smaller
        private void ZoomOut()
        {
            if ((pictureBox1.Width > (panel1.Width / MINMAX)) &&
                (pictureBox1.Height > (panel1.Height / MINMAX)))
            {
                pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
                pictureBox1.Width = Convert.ToInt32(pictureBox1.Width / ZOOMFACTOR);
                pictureBox1.Height = Convert.ToInt32(pictureBox1.Height / ZOOMFACTOR);
            }
        }

        #endregion

        #region Mouse Events
        private void pictureBox1_MouseWheel(object sender, MouseEventArgs e)
        {
            if (e.Delta < 0)
            {
                ZoomOut();
            }
            else
            {
                ZoomIn();
            }
        }
        private void pictureBox1_MouseEnter(object sender, EventArgs e)
        {
            if (pictureBox1.Focused == false)
            {
                pictureBox1.Focus();
            }
        }
        private void pictureBox1_MouseLeave(object sender, EventArgs e)
        {
            this.panel1.Focus(); // give the form focus instead
        }
        #endregion

        #region Button Events
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            ZoomIn();
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            ZoomOut();
        }
        #endregion
    }
}

Answer : How to read XML

May I be so bold as to suggest that the title suggests that the question is how to effectively read the XML file, to get that information.

Like, using an System.Xml.XmlDocument, and an XPath expression.

Example:

XmlDocument document = new XmlDocument();
document.Load(fileName);

XmlNodeList nodeList = document.SelectNodes("//Map");
Random Solutions  
 
programming4us programming4us