Solved

Search in an XmlDoc the text that the user inputs!

Posted on 2014-02-25
2
176 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 25

Accepted Solution

by:
Shaun Kline earned 350 total points
Comment Utility
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
Comment Utility
thanks a lot...
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

743 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

16 Experts available now in Live!

Get 1:1 Help Now