Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Search in an XmlDoc the text that the user inputs!

Posted on 2014-02-25
2
Medium Priority
?
186 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 27

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
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.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

609 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