Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7773241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T17:05:23+00:00 2026-06-01T17:05:23+00:00

I would like to know how to find a string in XML file. Say

  • 0

I would like to know how to find a string in XML file.

Say this is the XML file i have (these are the SQL server instances btw, irrelevant)

<?xml version="1.0" encoding="utf-8" ?>
<Servernames>
    <loc country="Lockheed">
        <Servername>instance1\server1</Servername>
        <Servername>instance2\server2</Servername>
        <Servername>10.90</Servername>
    </loc>
    <loc country="SouthAmerica">
        <Servername>Hide your heart</Servername>
        <Servername>Bonnie Tyler</Servername>
        <Servername>10.0</Servername>
    </loc>
    <loc country="Britian">
        <Servername>Greatest\Hits</Servername>
        <Servername>Dolly\Parton</Servername>
        <Servername>this\is</Servername>
    </loc>
</Servernames>

So what happens is i get a string from the user in any format say for example i only get instance and then i want the listbox to display all the servernames that start with server in the above case it will be

instance1\server1
instance2\serve2

and so on..
Not sure how to achieve this, do i have to open stream reader or just get a string and browser thru the xml file?

UPDATED

private void button1_Click(object sender, RoutedEventArgs e)
{
    textBox1.Clear();
    string fileName = "c:\\users\\xxxx\\documents\\visual studio 2010\\Projects\\WpfApplication2\\WpfApplication2\\XML.xml";

        var doc = XDocument.Load(fileName);
        var findString = "Server";

        var results = doc.Element("Servernames").Descendants("Servername").Where(d => d.Value.Contains(findString)).Select(d => d.Value);
        listBox1.Items.Add(results.ToString());
        textBox1.Text = results.ToString();
}

i am simply getting this in the text box :System.Linq.Enumerable+WhereSelectEnumerableIterator`2[System.Xml.Linq.XElement,System.String]

enter image description here

strong textUPDATE2

.cs file code

private void button1_Click(object sender, RoutedEventArgs e)
{
textBox1.Clear();

        string fileName = "c:\\users\\xxxxx\\documents\\visual studio 2010\\Projects\\WpfApplication2\\WpfApplication2\\XML.xml";

        var doc = XDocument.Load(fileName);
        var findString = "Server";

        var results = doc.Element("Servernames").Descendants("Servername").Where(d => d.Value.Contains(findString)).Select(d => d.Value);

        Servers = new ObservableCollection<string>(results);

        MessageBox.Show("THis is loaded");

    }

XAML looks like this

<ListBox   Height="200" HorizontalAlignment="Left" Margin="200,44,0,0" x:Name="ListBox1" VerticalAlignment="Top" Width="237">

enter image description here

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T17:05:24+00:00Added an answer on June 1, 2026 at 5:05 pm

    You could do something like this, assuming you load the XML into an XDocument.

    string fileName = "c:\\users\\xxxxx\\documents\\visual studio 2010\\Projects\\WpfApplication2\\WpfApplication2\\XML.xml";
    
    var doc = XDocument.Load(fileName);
    var findString = "server";
    
    var results = doc.Element("Servernames").Descendants("Servername").Where (d => d.Value.Contains(findString)).Select (d => d.Value);
    

    Edit – WPF Example

    XAML:

    <ListBox ItemSource="{Binding Servers}"/>
    

    DataContext:

    public ObservableCollection<string> Servers {get; set;}
    
    string fileName = "c:\\users\\xxxxx\\documents\\visual studio 2010\\Projects\\WpfApplication2\\WpfApplication2\\XML.xml";
    
    var doc = XDocument.Load(fileName);
    var findString = "server";
    
    var results = doc.Element("Servernames").Descendants("Servername").Where (d => d.Value.Contains(findString)).Select (d => d.Value);
    
    Servers = new ObservableCollection<string>(results);
    

    Edit2 – WPF Example without datacontext. You may want to read up on MVVM approach, as it is becoming the standard for WPF devleopment. Use this in the interim.

    XAML:

    <ListBox x:Name="ListBox1" />
    

    DataContext:

    string fileName = "c:\\users\\xxxxx\\documents\\visual studio 2010\\Projects\\WpfApplication2\\WpfApplication2\\XML.xml";
    
    var doc = XDocument.Load(fileName);
    var findString = "server";
    
    var results = doc.Element("Servernames").Descendants("Servername").Where (d => d.Value.Contains(findString)).Select (d => d.Value);
    
    foreach(string result in results)
    {
         ListBox1.Items.Add(result);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey I have an XML file and I would like to navigate to a
I would like to know where I can find the list of properties settable
I would like to know if there's an efficient algorithm to find the greatest
I would like to know if you find the following pattern meaningful in domain
I would like to know if there is the way to find out which
Hi I would like to know how I should connect to the external SQL
i would like know some reference. I know i can googling it. but prefer
Would like to know what a programmer should know to become a good at
Would like to know the c# code to actually retrieve the IP type: Static
Would like to know how to integarate cruise control with maven? Cruise Control comes

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.