在XML中查找字符串

时间:2012-04-13 01:30:50

标签: c# xml string

我想知道如何在XML文件中找到一个字符串。

假设这是我拥有的XML文件(这些是SQL服务器实例btw,无关紧要)

<?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>

所以会发生什么呢?我从任何格式的用户那里得到一个字符串,例如我只获取实例然后我希望列表框显示所有以服务器开头的服务器名称,在上述情况下它将是

INSTANCE1 \ server1的
instance2 \ serve2

等等.. 不知道如何实现这一点,我是否必须打开流阅读器或只是通过xml文件获取字符串和浏览器?

已更新

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();
}

我只是在文本框中输入: System.Linq.Enumerable + WhereSelectEnumerableIterator`2 [System.Xml.Linq.XElement,System.String]

enter image description here

强文 UPDATE2

.cs文件代码

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看起来像这样

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

enter image description here

2 个答案:

答案 0 :(得分:4)

您可以执行类似的操作,假设您将XML加载到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);

编辑 - WPF示例

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 - 没有datacontext的WPF示例。您可能想要了解MVVM方法,因为它正在成为WPF开发的标准。在过渡期间使用它。

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);
}

答案 1 :(得分:1)

您可以像这样使用XmlDocument:

string xml = "<your xml>";
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
foreach (XmlNode xmlLoc in doc.DocumentElement.SelectNodes("loc"))
{
    foreach (XmlNode xmlServername in xmlLoc.SelectNodes("Servername"))
    {
        Debug.WriteLine(string.Format("Servername={0}", xmlServername.InnerText));
        // xmlServername.InnerText will be \instance1\server1, etc.
        // UPDATE: add item to listbox
        listBox1.Items.Add(xmlServername.InnerText);
    }
}