Smyken
asked on
foreach loop help needed
Hello
Im currently reading the XML file using the code below.
Currently I loop the (thumb) node and get the image path to the picturebox.Image and to picturebox.Tag
How can I loop through the other node (original) at same time and get it's innertext assign to the picturebox.Tag instead ?
Im currently reading the XML file using the code below.
Currently I loop the (thumb) node and get the image path to the picturebox.Image and to picturebox.Tag
How can I loop through the other node (original) at same time and get it's innertext assign to the picturebox.Tag instead ?
Xml file:
<movie>
<poster size="original">fullframe.jpg</poster>
<poster size="mid">mid_44.jpg</poster>
<poster size="thumb">thumber.jpg</poster>
<poster size="original">big.jpg</poster>
<poster size="mid">middle.jpg</poster>
<poster size="thumb">little.jpg</poster>
</movie>
//C# Code
XmlNode node;
XmlDocument xdoc = new XmlDocument();
xdoc.Load(@"c:\myxml.xml");
node = xdoc.DocumentElement;
XmlNodeList nlMovie = node.SelectNodes("/movie");
//Get Posters
XmlNodeList xnl = nlMovie[0].SelectNodes("poster[@size='thumb']");
XmlNodeList xnl2 = nlMovie[0].SelectNodes("poster[@size='original']"); // How do I get this nodes innertext into the pb.Tag ???
foreach (XmlNode x in xnl)
{
string Poster = x.InnerText;
PictureBox pb = new PictureBox();
pb.BorderStyle = BorderStyle.FixedSingle;
pb.Size = new Size(92, 135);
pb.SizeMode = PictureBoxSizeMode.StretchImage;
pb.Tag = Poster; // Need the ("poster[@size='original']") values here !?!?!
Image img = Image.FromFile("Poster");
pb.Image = img;
// Add Pictureboxes to FlowLayoutpanel
Poster_flp.Controls.Add(pb);
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Think I solved it !
Works now without any problems.
Could you please take a look at my altered code and say if its ok to use or if anything should be changed??
Works now without any problems.
Could you please take a look at my altered code and say if its ok to use or if anything should be changed??
XmlNode node;
XmlDocument xdoc = new XmlDocument();
xdoc.Load(@"c:\myxml.xml");
node = xdoc.DocumentElement;
XmlNodeList nlMovie = node.SelectNodes("/movie");
//Get Posters
XmlNodeList xnl = nlMovie[0].SelectNodes("poster[@size='thumb']");
XmlNodeList xnl2 = nlMovie[0].SelectNodes("poster[@size='original']");
for (int i = 0; i < xnl.Count; i++)
{
if (xnl[i].InnerText != null)
{
string Poster = xnl[i].InnerText;
PictureBox pb = new PictureBox();
pb.BorderStyle = BorderStyle.FixedSingle;
pb.Size = new Size(92, 135);
pb.SizeMode = PictureBoxSizeMode.StretchImage;
pb.Tag = xnl2[i].InnerText;
Image img = Image.FromFile("Poster");
pb.Image = img;
// Add Pictureboxes to FlowLayoutpanel
Poster_flp.Controls.Add(pb);
}
}
Yeah that looks good. It's essentially what I had. The first error I had was just a typo and the second error was that I was not paying attention and was assuming "xnl" was an array, hence the use of xnl.Length. One thing though, on line 24 you have "Image img = Image.FromFile("Poster");" , I believe "Poster" should be the filename of the image you want to open. So I'm guessing it should be the inner text of either xnl or xnl2. Otherwise looks good.
Actually I see what you where doing with line 24. The variable Poster is a string, so there is no need to include in quotes on line 24. Otherwise you will be trying to open a file simply called "Poster".
ASKER
You made 2 errors:
error 1:
line
13. string Poster = xn1[i].InnerText; should be:
13. string Poster = xnl[i].InnerText;
error 2:
I get this when I try to run your code:
Error 1 'System.Xml.XmlNodeList' does not contain a definition for 'Length' and no extension method 'Length' accepting a first argument of type 'System.Xml.XmlNodeList' could be found (are you missing a using directive or an assembly reference?)
I then tried to change:
line
11. for (int i = 0; i < xnl.Length; i++) //to:
11. for (int i = 0; i < xnl.ToString().Length; i++)
Now it runs untill the last picture then crashes whit this statement:
NullRefernceException was unhandled.