Question regarding Foreach loop with Listview Items

Here is my problem. I am sure it is probably something simple.

Say my listview has 6 items in it.

The problem I am running into is the whole last item #5 or 6 is not getting written out to the XML if I delete either #5 or 6 from the listview.  If I delete item 1-4 it works fine. When I delete 5 or 6 then the whole last block does not get written out.

The way my XML file should work is item 1+2 contain the same info minus 3 fields so they are linked as one. They are all paired up 3+4 and 5+6 What should happen is if I delete #5 the XML should show 1+2, 3+4, and 5 alone.

Can someone tell me what I am doing wrong in this loop. I have been working on it for a while.

         string[] PreviousRowArray = null;

         foreach (ListViewItem row in listViewFount.Items)
         {
            string CurrentRow = "";

            for (int i=0 ; i<row.SubItems.Count ;i++)
            {
               CurrentRow = CurrentRow + row.SubItems[i].Text + " ";
            }

            string[] CurrentRowArray = CurrentRow.Split(' ');
               
            if (PreviousRowArray != null)
            {
               if ((CurrentRowArray[1].ToString() == PreviousRowArray[1].ToString()) &&
                  (CurrentRowArray[1].ToString() == PreviousRowArray[1].ToString()))
               {
                  //Here I write out the CurrentRowArray information combined with
                  //the PreviousRowArray. Removed for ease of reading
                 
                  PreviousRowArray = null;
                  CurrentRowArray  = null;
               }
               else
               {
        //Here I write only the pervious line out to the
                  //XML file. Removed for ease of reading

                  PreviousRowArray  = CurrentRowArray;
               }
            }
            else//Move currentrow to previous

               PreviousRowArray = CurrentRow.Split(' ');
         }

         //this is the end of the document
         textwriter.WriteEndDocument();              
         textwriter.Flush();
         textwriter.Close();

      }


LVL 2
NewMom2BrandonAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

wlfsCommented:
Hi, I only had a quick look up to now. What catched my eyes is the if condition
               if ((CurrentRowArray[1].ToString() == PreviousRowArray[1].ToString()) &&
                  (CurrentRowArray[1].ToString() == PreviousRowArray[1].ToString()))
The two sides of the && are identical. I guess it should be
               if ((CurrentRowArray[0].ToString() == PreviousRowArray[0].ToString()) &&
                  (CurrentRowArray[1].ToString() == PreviousRowArray[1].ToString()))
Does this solve your problem?
0
NewMom2BrandonAuthor Commented:
hmm that was a good problem (duh kicking myself) but it still doesn't solve my actual issue.
0
junglerover77Commented:
I think that's obvious you item #5 will Not be written out if #6 is removed.

Just imagine:
when the loop goes to #4, because #3 and #4 matches, the following code will be executed:
                  //Here I write out the CurrentRowArray information combined with
                  //the PreviousRowArray. Removed for ease of reading
                 
                  PreviousRowArray = null;
                  CurrentRowArray  = null;

so, when the loop goes to #5, the value of "PreviousRowArray" is null. so it goes to:

            else//Move currentrow to previous

               PreviousRowArray = CurrentRow.Split(' ');

then the loop is ended because #5 is already the last element. So, nothing will happen.

Just add the following codes before your last 3 statements (after the loop), just like this:

         if (PreviousRowArray != null) {
         //Here I write only the pervious line out to the
                  //XML file. Removed for ease of reading
         }

         //this is the end of the document
         textwriter.WriteEndDocument();              
         textwriter.Flush();
         textwriter.Close();

Jungle
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
NewMom2BrandonAuthor Commented:
thank you!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.