?
Solved

Search in an XmlDoc the text that the user inputs!

Posted on 2014-02-25
2
Medium Priority
?
184 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 26

Accepted Solution

by:
Shaun Kline earned 1400 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

777 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