tags:

views:

497

answers:

1

Hi all,

I am a beginner and I want to select an XML file and load it's elements (i.e., a file with tags such as <item>, <title>, etc.) into a DropDownList.

In a separate TextBox it should count and show how many times a particular tag or element is present.

When a particular element is selected from the DropDownList, the contents, descriptions including the sub-elements in that element should be displayed into a TextBox.

I working with ASP.NET and C#.

................................................................................................................

Thanx a lot for all who have read and replied to my Answer. Iam using ASP.Net 3.5 only.

Precisly I would explain.........

Example of an XML File An XML File

 <Persons>

Paxton Munich 29 Mike Orlando 33 Ella LA 13 Zach Munich 32 Ingrid Oslo 63

First, I browse and select this XML File.

Second, Load the Xml Tags into a DropDownlist. ie. the dropdownlist should contain Persons, Person, Name, City & Age.

   Now on selecting say **Person** the textbox should Display Message as "The XML File has: 5 Person tags" and SHould display all the Contents under Person tags including its subtags.

Eg:

<Person>
<Name>Paxton</Name>
<City>Munich</City>
<Age>29</Age>

Mike Orlando 33 Ella LA 13 Zach Munich 32 Ingrid Oslo 63

this way it should display for any other tags also, such as Name, City, Age, Persons.

      In the same way for any other XML File that is selected.

My pblm here is, I couldn't load XML tags into a Dropdownlist.

Plz Help me

+1  A: 

Your question is not 100% clear but lets take a shot at it.

Firstly lets create a class that has a ElementName property and an XElement property that overides the ToString() method that we can use to populate the dropdown...

  public class displayclass
  {
    public string ElementName { get; set; }
    public XElement Element { get; set; }
    public override string ToString()
    {
      return ElementName;
    }
  }

Then we can easily add the Elements into the Combobox with the Element Name as the text, like so

//Some Sample Xml
  XElement xe = new XElement("Root",
    new XElement("Customer",
      new XAttribute("Name", "John Smith"),
      new XAttribute("CreditLimit", 1500)),
    new XElement("Employee",
      new XAttribute("Name", "Fred Nerk")),
    new XElement("Employee",
      new XAttribute("Name", "Sally Silverton")));

  var elemList = from x in xe.Elements()
                 select new displayclass { ElementName = x.Name.ToString(), Element = x };
  foreach (var item in elemList)
  {
    comboBox1.Items.Add(item);
  }

Now Lets add the count of the Elements into a Text box using a group by clause on our list...

  var qry = from dispObj in elemList
            group dispObj by dispObj.ElementName;
  StringBuilder sb = new StringBuilder();
  foreach (var grp in qry)
  {
    int count = grp.Count();
    sb.AppendLine(string.Format("{0}({1})", grp.Key,grp.Count()));
  }
  textBox1.Text = sb.ToString();

And finally lets add an event handler to the selected index change event of the Combobox to display the contents of the element...

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
  displayclass disp = comboBox1.SelectedItem as displayclass;
  if (disp != null)
  {
    textBox2.Text = disp.Element.ToString();
  }
}

I think this satisfies your reqs as listed above.

Tim Jarvis
that'll work provided he's on .NET 3.5 or higher - Linq-to-XML didn't exist in .NET 2.0 / 3.0 :-(
marc_s
Sure, but I would say to anyone working with XML, get on 3.5 so that you can use Linq to XML.
Tim Jarvis