1

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

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

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

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

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]

在此处输入图像描述

强文本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">

在此处输入图像描述

4

2 回答 2

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}"/>

数据上下文:

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 示例。您可能想阅读 MVVM 方法,因为它正在成为 WPF 开发的标准。在此期间使用它。

XAML:

<ListBox x:Name="ListBox1" />

数据上下文:

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);
}
于 2012-04-13T01:39:51.263 回答
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);
    }
}
于 2012-04-13T01:48:11.087 回答