【发布时间】:2017-10-05 01:13:16
【问题描述】:
<?xml version="1.0" ?>
<aliasSection>
<aliases>
<clear />
<add
name="MAIN"
server="JAG8MTO\SQLEXPRESS"
database="RMain"
trustedConnection="false" />
<add
name="DEMO"
server="JAG8MTO\SQLEXPRESS"
database="RDemo"
trustedConnection="false" />
</aliases>
</aliasSection>
在上面的 xml 文档中,我需要搜索一个别名,然后返回服务器和数据库属性。
这是我第一次使用 xml 文档,我很难理解它。
到目前为止,我可以找到别名部分,但我不知所措,必须从这里开始。
public void Read(string fileName)
{
XDocument doc = XDocument.Load(fileName);
foreach (XElement el in doc.Root.Elements())
{
foreach (XAttribute attr in el.Attributes())
if (attr.ToString() == "aliases")
{
foreach (XElement element in el.Elements())
listBox1.Items.Add(element.Name + " " + element.Value);
foreach (XAttribute attrib in el.Attributes())
listBox1.Items.Add(attrib.Value);
}
}
}
【问题讨论】:
-
aliases是元素,不是属性