• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 187
  • Last Modified:

Search in an XmlDoc the text that the user inputs!

hi again guys... im looking this time a code for searching the text that the user inputs in a textbox, im using a code that its working but what I want is to reduce the code, here is the code:

string code = textBox3.Text;

            XElement xelement = XElement.Load(@"C:\books.xml");

            if (code == "7582")
            {
                var sCode = from nm in xelement.Elements("book")
                            where (string)nm.Element("code") == "7582"
                            select nm;
                foreach (XElement xEle in sCode)
                    MessageBox.Show(Convert.ToString(xEle), "Success");
            }
           
            else
            {
                if (code == "7043")
                {
                    var sCode = from nm in xelement.Elements("book")
                                where (string)nm.Element("code") == "7043"
                                select nm;
                    foreach (XElement xEle in sCode)
                        MessageBox.Show(Convert.ToString(xEle), "Success");
                }

                else
                {
                    if (code == "")
                    {
                        MessageBox.Show("Enter a Valid Code", "Error");
                    }
                    else
                    {
                        MessageBox.Show("Not Found", "Error");
                    }
                }
            }

thanks in advance..!!!
0
Conrado ZAVALA
Asked:
Conrado ZAVALA
1 Solution
 
Shaun KlineLead Software EngineerCommented:
You can combine the two "if (code == ...) blocks together and use your code variable in place of the hard coded value.

            if (code == "7582" || code == "7043")
            {
                var sCode = from nm in xelement.Elements("book")
                            where (string)nm.Element("code") == code
                            select nm;
                foreach (XElement xEle in sCode)
                    MessageBox.Show(Convert.ToString(xEle), "Success");
            }

Open in new window

0
 
Conrado ZAVALAAuthor Commented:
thanks a lot...
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now