ASP.NET Forums/Data Access/XML and XmlDataSource Control/How to find listbox numeric value using linq to xml

How to find listbox numeric value using linq to xml [Answered]RSS

4 replies

Last post Dec 31, 2010 06:23 PM by kbainey1

  • kbainey1 kbainey1

    Member

    48 Points

    425 Posts

    Re: How to find listbox numeric value using linq to xml

    Dec 29, 2010 04:30 PM|kbainey1|LINK

    Sorry I am not able to do it that way. To make things simple, I just want to be able to receive the "Id" from the xml response selected item.

    ("ContactId", ddName.SelectedValue))    

     

    So if I select Jerry then I would like ddName.SelectedValue be set to Id 1646 as below.

  • <Contact>  
  •     <Relationship />  
  •     <Id>1646</Id>  
  •     <Phone>7053602600x8833</Phone>  
  •     <Email>jstpi@timmins.ca</Email>  
  •     <FirstName>Jerry</FirstName>  
  •     <LastName>St. Pierre</LastName>  
  • </Contact>  

     

     

  • Decker Dong - MSFT Decker Dong...

    All-Star

    94120 Points

    18111 Posts

    Re: How to find listbox numeric value using linq to xml

    Dec 31, 2010 12:17 AM|Decker Dong - MSFT|LINK

    var result = from e in XDocument.Parse("xml string directly")/ .Load("xml path and file name").Descades("Contact")

                      where e.Element("FirstName").Value.Equals("Your own value here")

                       select e.Element("Id").Value;

  • kbainey1 kbainey1

    Member

    48 Points

    425 Posts

    Re: How to find listbox numeric value using linq to xml

    Dec 31, 2010 06:23 PM|kbainey1|LINK

    Thank you for all your help guys. I got it to work now.
    protected void Page_Load(object sender, EventArgs e)
            {
    //generate dropdownlist box with first and last name
                if (!Page.IsPostBack)
                {
                    try
                    {
                        lblWelcomeMessage.Text = ("Hello" + " " + HttpContext.Current.User.Identity.Name + ", " + "Please submit your new ticket ");
    
    
                        //set focus to fields at startup
                        btnSubmit.Focus();
    
    
                        //XML Request
                        var xmlRequest = new XElement("FindPartnerContactsAction", new XAttribute(XNamespace.Xmlns + "xsi", "http://www.w3.org/2001/XMLSchema-instance"), new XAttribute(XNamespace.Xmlns + "xsd", "http://www.w3.org/2001/XMLSchema"),
                                                      new XElement("CompanyName", "training"),
                                                      new XElement("IntegrationLoginId", "login"),
                                                      new XElement("IntegrationPassword", "password"),
                                                      new XElement("Conditions", "CompanyName = \"" + "My Company Name" + "\""))
                         .ToString();
    
    
                        //find the response of the contact entered
                        var proxy = new MyWebService.integration_io();
                        var xmlResponse = proxy.ProcessClientAction(xmlRequest);
                        var parsedXmlResponse = XElement.Parse(xmlResponse);
    
    
                        var contacts = from contact in parsedXmlResponse.Descendants("Contact")
                               select new
                               {
                                   Id = contact.Element("ContactRecID").Value,
                                   Name = contact.Element("FirstName").Value + " " + contact.Element("LastName").Value
                               };
    
    
                        ddName.DataSource = contacts;
                        ddName.DataTextField = "Name" ;
                        ddName.DataValueField = "Id";
                        ddName.DataBind();
    
    
                    }
                    catch (Exception Exc)
                    {
                        Response.Write("Error: " + Exc.Message);
                    }
                }
    }
    
    
       
    
    protected void ddName_SelectedIndexChanged(object sender, EventArgs e)
            {
                try
                {
                    var xmlRequest = new XElement("FindPartnerContactsAction", new XAttribute(XNamespace.Xmlns + "xsi", "http://www.w3.org/2001/XMLSchema-instance"), new XAttribute(XNamespace.Xmlns + "xsd", "http://www.w3.org/2001/XMLSchema"),
                                             new XElement("CompanyName", "training"),
                                             new XElement("IntegrationLoginId", "login"),
                                             new XElement("IntegrationPassword", "password"),
                                             new XElement("Conditions", "CompanyName = \"" + "My Company" + "\""))
                     .ToString();
    
    
    
    
    
    
    
    
                    //find the response of the contact entered
                    var proxy = new MyWebService.integration_io();
                    var xmlResponse = proxy.ProcessClientAction(xmlRequest);
                    var parsedXmlResponse = XElement.Parse(xmlResponse);
    
    
                    //find the ID of the contact searched from dropdownlistbox
                    var contacts = from contact in parsedXmlResponse.Descendants("Contact")
                                   where contact.Element("ContactRecID").Value.Equals(ddName.SelectedValue)
                                   select new
                                   {
                                       Phone = contact.Element("Phone").Value,
                                       Email = contact.Element("Email").Value,
                                       Address = contact.Element("AddressLine1").Value,
                                       City = contact.Element("City").Value,
                                       Province = contact.Element("State").Value,
                                       PostalCode = contact.Element("Zip").Value,
                                   };
    
    
                    foreach (var contact in contacts)
                    {
                        txtContactPhone.Text = contact.Phone;
                        txtContactEmail.Text = contact.Email;
                        txtPrimaryAddress.Text = contact.Address;
                        txtCity.Text = contact.City;
                        txtProvince.Text = contact.Province;
                        txtPostalCode.Text = contact.PostalCode;
                    }
                }
                catch (Exception Exc)
                {
                    Response.Write("Error: " + Exc.Message);
                }
            }
    
    
    These links where also of help:
    http://www.dreamincode.net/forums/topic/134850-listbox-item-id/
    http://www.joe-stevens.com/2010/01/08/linq-to-xml-tutorial/
    http://windowsclient.net/blogs/pirate11/archive/2010/01/13/linq-with-xml-database.aspx
    http://www.dbtutorials.com/retrieve/linq-to-xml-cs.aspx
    http://www.aspnettutorials.com/tutorials/xml/linq-to-xml-adding-cs.aspx


     

  • ‹ Previous Thread|Next Thread ›