[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

foreach loop help needed

Posted on 2009-04-29
5
Medium Priority
?
219 Views
Last Modified: 2013-12-17
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 ?



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);
            }

Open in new window

0
Comment
Question by:Smyken
  • 3
  • 2
5 Comments
 
LVL 4

Accepted Solution

by:
my2eggs earned 2000 total points
ID: 24265613
The simplest way would be to not use a foreach but instead use a standard for loop. Then you can use the index used to reference the current (thumb) node to also reference it's corresponding (original) node.
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.Length; i++)
{
  string Poster = xn1[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);
}

Open in new window

0
 
LVL 1

Author Comment

by:Smyken
ID: 24265813
Got it partially working but still no go 8=(

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.
0
 
LVL 1

Author Comment

by:Smyken
ID: 24265911
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??

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);
  }
}

Open in new window

0
 
LVL 4

Expert Comment

by:my2eggs
ID: 24265991
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.
0
 
LVL 4

Expert Comment

by:my2eggs
ID: 24266025
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".
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question