Previous Tip  |  Next Tip  |  Design Tips   | [Bill's Home]

210. XML and .NET

In Design tip 208 I showed how to read-in images which are defined in XML into Flash. This is a simple tip to show how this is done in .NET. Download solution [Solution file]:

The forward button is then:

private void button1_Click(object sender, System.EventArgs e)
{
System.Drawing.Image imgInFile;
this.imagePointer++;

if (imagePointer>imageNames.Count-1) imagePointer=0;

string s = (string)this.imageNames[imagePointer];
imgInFile=Image.FromFile(s);

pictureBox1.Image=imgInFile;

}

and the back button with:

private void button2_Click(object sender, System.EventArgs e)
{
System.Drawing.Image imgInFile;
this.imagePointer--;

if (imagePointer<0) imagePointer=imageNames.Count-1;

string s = (string)this.imageNames[imagePointer];
imgInFile=Image.FromFile(s);

pictureBox1.Image=imgInFile;
}

The main other code is:

public void readXMLfile()
{


XmlTextReader reader = new XmlTextReader ("image.xml");
reader.WhitespaceHandling=WhitespaceHandling.None;

while (reader.Read())
{
if (reader.NodeType== XmlNodeType.Element)
{
if (reader.Name.ToLower()=="im")
{

try
{
reader.Read();
this.imageNames.Add(reader.Value.ToLower());

}
catch{} ;
}
}
}
scramble(imageNames);

load_image(pictureBox1,(string)imageNames[0]);
load_image(pictureBox1,(string)imageNames[1]);


}
public void load_image(PictureBox i, string name)
{
System.Drawing.Image imgInFile;

imgInFile=Image.FromFile(name);

i.Image=imgInFile;

}

The XML file is then:

<images>
<im> 1.jpg </im>
<im> 2.jpg </im>
<im> 3.jpg </im>
</images>

These images are:

and:

and:

The result is: