使用 Xml.Xpath 和 Xml.Linq 读取 XML 文件
Read XML file using Xml.Xpath and Xml.Linq
我正在尝试阅读以下 XML 文件:
<?xml version="1.0" encoding="utf-8"?>
<Connection name="default">
<Attribute name="server" value="localhost" />
<Attribute name="database" value="mp" />
<Attribute name="uid" value="root" />
<Attribute name="password" value="m123" />
<Attribute name="code" value="MK" />
</Connection>
使用以下代码:
var doc = XDocument.Load("DBConnect.xml");
var values = doc.XPathSelectElements("//Connection[@name='default']");
foreach (var item in values)
{
foreach (var att in item.Elements("Attribute"))
{
_server = att.Attribute("server").Value;
_database = att.Attribute("database").Value;
_uid = att.Attribute("uid").Value;
_password = att.Attribute("password").Value;
_code = att.Attribute("code").Value;
}
}
但是,我似乎没有得到正确的输出。我收到一条错误消息,告诉我 _server
为空。知道为什么吗?我认为我没有正确引用我想要掌握的 XML 属性值。
没有名为 "server"、"database"、"uid"、"password" 或 "code" 的属性。它们都是名为 "name" 的属性的值,这意味着您调用 Attribute("name") 而不是它的值。
例如
var attrVal = attr.Attribute("name").Value;
if (attrVal == "server")
_server = attrVal.Value;
// ... etc
如果上述方法不起作用,请尝试此操作。
var attrVal = attr.Attribute("name").Value;
if (attrVal == "server")
_server = attr.Attribute("value").Value;
// ... etc
问题是您正在遍历名为 Attribute
的元素并试图在其中找到 XML 属性。您的逻辑会表明每个 XML 元素上应该有所有 5 个 XML 属性,称为 Attribute。尝试改用这样的代码:
var values = doc.XPathSelectElements("//Connection[@name='default']");
_server = values.Elements("Attribute") //look for an element called Attribute
.Where(el => el.Attribute("name").Value == "server") //which has attribute name="server"
.Single() //there should be only single element
.Attribute("value").Value; //get the value of attribute value
// Repeat that for all other attributes (_database, _uid, etc.)
您可以使用的一种干净的方法:
var values = doc.XPathSelectElements("//Connection[@name='default']")
.Single()
.Elements("Attribute")
.ToDictionary(el => (string)el.Attribute("name"),
el => (string)el.Attribute("value"));
_server = values["server"];
_database = values["database"];
_uid = values["uid"];
_password = values["password"];
_code = values["code"];
values
是一个 IDictionary<string, string>
,因此您可以使用它来检索添加的任何附加属性,而无需修改原始 LINQ 代码。
我正在尝试阅读以下 XML 文件:
<?xml version="1.0" encoding="utf-8"?>
<Connection name="default">
<Attribute name="server" value="localhost" />
<Attribute name="database" value="mp" />
<Attribute name="uid" value="root" />
<Attribute name="password" value="m123" />
<Attribute name="code" value="MK" />
</Connection>
使用以下代码:
var doc = XDocument.Load("DBConnect.xml");
var values = doc.XPathSelectElements("//Connection[@name='default']");
foreach (var item in values)
{
foreach (var att in item.Elements("Attribute"))
{
_server = att.Attribute("server").Value;
_database = att.Attribute("database").Value;
_uid = att.Attribute("uid").Value;
_password = att.Attribute("password").Value;
_code = att.Attribute("code").Value;
}
}
但是,我似乎没有得到正确的输出。我收到一条错误消息,告诉我 _server
为空。知道为什么吗?我认为我没有正确引用我想要掌握的 XML 属性值。
没有名为 "server"、"database"、"uid"、"password" 或 "code" 的属性。它们都是名为 "name" 的属性的值,这意味着您调用 Attribute("name") 而不是它的值。 例如
var attrVal = attr.Attribute("name").Value;
if (attrVal == "server")
_server = attrVal.Value;
// ... etc
如果上述方法不起作用,请尝试此操作。
var attrVal = attr.Attribute("name").Value;
if (attrVal == "server")
_server = attr.Attribute("value").Value;
// ... etc
问题是您正在遍历名为 Attribute
的元素并试图在其中找到 XML 属性。您的逻辑会表明每个 XML 元素上应该有所有 5 个 XML 属性,称为 Attribute。尝试改用这样的代码:
var values = doc.XPathSelectElements("//Connection[@name='default']");
_server = values.Elements("Attribute") //look for an element called Attribute
.Where(el => el.Attribute("name").Value == "server") //which has attribute name="server"
.Single() //there should be only single element
.Attribute("value").Value; //get the value of attribute value
// Repeat that for all other attributes (_database, _uid, etc.)
您可以使用的一种干净的方法:
var values = doc.XPathSelectElements("//Connection[@name='default']")
.Single()
.Elements("Attribute")
.ToDictionary(el => (string)el.Attribute("name"),
el => (string)el.Attribute("value"));
_server = values["server"];
_database = values["database"];
_uid = values["uid"];
_password = values["password"];
_code = values["code"];
values
是一个 IDictionary<string, string>
,因此您可以使用它来检索添加的任何附加属性,而无需修改原始 LINQ 代码。