Asim Zaidi
Asim Zaidi

Reputation: 28284

how to get the count and pop a message box

I am trying to get the message box to show the elements but messagebox will not pop when I run the application

string xml = @"<?xml version='1.0' encoding='UTF-8'?>
<widgets>
    <widget>
        <url>~/Portal/Widgets/ServicesList.ascx</url>
        <castAs>ServicesWidget</castAs>
        <urlType>ascx</urlType>
        <parameters>
            <PortalCategoryId>3</PortalCategoryId>
        </parameters>
    </widget>
    <widget>
        <url>www.omegacoder.com</url>
        <castAs>ServicesWidget</castAs>
        <urlType>htm</urlType>
        <parameters>
            <PortalCategoryId>41</PortalCategoryId>
        </parameters>
    </widget>
</widgets>";

XDocument loaded = XDocument.Parse( xml );

var widgets = from x in loaded.Descendants( "widget" )
              select new
              {
                  URL = x.Descendants( "url" ).First().Value,
                  Category = x.Descendants( "PortalCategoryId" ).First().Value
              };

MessageBox.Show("one");
foreach ( var wd in widgets ){
MessageBox.Show("two");

 }

MessageBox.Show("one"); shows up. MessageBox.Show("two"); never pops up

Also what if I wanna see a count of widgets> I am new to C# thanks

Upvotes: 1

Views: 1288

Answers (3)

Lukasz M
Lukasz M

Reputation: 5723

You can replace your LINQ query with:

var widgets = from x in loaded.Descendants("widgets").Descendants( "widget" )
          select new
          {
              URL = x.Descendants( "url" ).First().Value,
              Category = x.Descendants( "PortalCategoryId" ).First().Value
          };

UPDATE: Both versions of LINQ should work correctly. My mistake, Descendants can point not only to directly nested nodes, but to all the nodes in the subtree.

Probable issue cause: However, be aware that in order to show the second message box, you have to close the first one. I've just tested this and it worked.

Suggested solution: In order to be available to show multiple dialogs with messages, you can just create your own form class in the project, instantiate it and show it with Show() method i.e.: you can add a new Windows Form, call it MessageForm and use the following code:

//MessageBox.Show(new Form(), "one");
        MessageForm msgDlg = new MessageForm() { Message = "one" };
        msgDlg.Show(this);
        foreach (var wd in widgets)
        {
            //MessageBox.Show(new Form(), "two");
            MessageForm msgDlgS = new MessageForm() { Message = "two" };
            msgDlgS.Show(this);
        }

It should work as intended then.

Upvotes: 2

FastAl
FastAl

Reputation: 6280

If you try MessageBox.Show(widgets.Count().ToString()) my bets are it will say 0. It is defined behaviour of for loop to do NO iterations on a collection with 0 elements. So "two" is never displayed.

EDIT -

As to why it would be zero that's obviously a problem if it is! Maybe you need to loop thru from x in loaded.Descendants( "widgets/widget" ) ???

I don't remember if XDocument forces you to swallow past the documentelement or not like XMLDocument does.

Upvotes: 2

Renato Gama
Renato Gama

Reputation: 16519

Replace

MessageBox.Show("one");

by

MessageBox.Show(widgets.Count()); //.Count(), not .Count

Check if you have any elements to loop through!

Upvotes: 2

Related Questions