Solved

Search in an XmlDoc the text that the user inputs!

Posted on 2014-02-25
2
178 Views
Last Modified: 2014-02-25
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
Comment
Question by:Conrado ZAVALA
2 Comments
 
LVL 26

Accepted Solution

by:
Shaun Kline earned 350 total points
ID: 39886864
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
 

Author Closing Comment

by:Conrado ZAVALA
ID: 39886879
thanks a lot...
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now