从任意键路径检索 C# 中的 XML 值
Retrieving XML value in C# from arbitary key path
我有一个项目,我目前正在实现对通过文档键中的 arbitrary/user-defined 路径从 XML 文件读取值的支持。
例如,如果文档如下所示:
<information>
<machine>
<foo></foo>
<name>
test machine
</name>
<bar>spam</bar>
</machine>
</information>
然后用户可能想从 information/machine
中的 name
键检索值。
有没有一种方法可以使用 XDocument
/XPath
来查找用户想要的值,而无需在文档架构中使用 knowing/coding?
我最初的想法是通过使用 XElement
项的递归函数形式来处理文档,但我觉得应该有一个 simpler/cleaner 不需要我滚动我的解决方案自己的查找代码。
我也尝试过类似的方法
var doc = XDocument.Load("C:\Path\to\XML\file.xml");
// Split the parent keys string
XElement elem = doc.Root.XPathSelectElement("path/to/key");
if (elem != null && elem.Attribute("wantedKeyName") != null)
replace = elem.Attribute("wantedKeyName").Value;
但 elem
始终为空。我假设我定义路径或使用 XPathSelectElement 的方式有问题,但我还没有解决它。
static XmlNode SearchNode(XmlNodeList nodeList, string nodeName)
{
for (int i = 0; i < nodeList.Count; i++)
{
if (nodeList[i].Name == nodeName)
{
return nodeList[i];
}
if (nodeList[i].HasChildNodes)
{
XmlNode node = SearchNode(nodeList[i].ChildNodes, nodeName);
if (node != null)
{
return node;
}
}
}
return null;
}
static XmlNodeList SearchNodeByPath(XmlNodeList nodeList, string xPath)
{
for (int i = 0; i < nodeList.Count; i++)
{
var nodes = nodeList[i].SelectNodes(xPath);
if (nodes != null && nodes.Count > 0)
{
return nodes;
}
if (nodeList[i].HasChildNodes)
{
XmlNodeList innerNodes = SearchNodeByPath(nodeList[i].ChildNodes, xPath);
if (innerNodes != null && innerNodes.Count > 0)
{
return innerNodes;
}
}
}
return null;
}
这是使用方法:
var node = SearchNode(doc.ChildNodes, "compiler");
var node1 = SearchNodeByPath(doc.ChildNodes, "compilers/compiler");
我发现我使用 XPathSelectElement
的解决方案是正确的方法,我只需要在 path/to/key
字符串前加上 //
。
我最终使用的以下代码会执行此操作并去除值外部周围的所有空格(以防值位于与开始标记不同的行上。
// xml is a struct with the path to the parent node (path/to/key)
// and the key name to look up
// Split the parent keys string
XElement elem = doc.Root.XPathSelectElement("//" + xml.KeyPath);
if (elem != null && elem.Element(xml.Key) != null)
replace = elem.Element(xml.Key).Value.Trim();
我有一个项目,我目前正在实现对通过文档键中的 arbitrary/user-defined 路径从 XML 文件读取值的支持。
例如,如果文档如下所示:
<information>
<machine>
<foo></foo>
<name>
test machine
</name>
<bar>spam</bar>
</machine>
</information>
然后用户可能想从 information/machine
中的 name
键检索值。
有没有一种方法可以使用 XDocument
/XPath
来查找用户想要的值,而无需在文档架构中使用 knowing/coding?
我最初的想法是通过使用 XElement
项的递归函数形式来处理文档,但我觉得应该有一个 simpler/cleaner 不需要我滚动我的解决方案自己的查找代码。
我也尝试过类似的方法
var doc = XDocument.Load("C:\Path\to\XML\file.xml");
// Split the parent keys string
XElement elem = doc.Root.XPathSelectElement("path/to/key");
if (elem != null && elem.Attribute("wantedKeyName") != null)
replace = elem.Attribute("wantedKeyName").Value;
但 elem
始终为空。我假设我定义路径或使用 XPathSelectElement 的方式有问题,但我还没有解决它。
static XmlNode SearchNode(XmlNodeList nodeList, string nodeName)
{
for (int i = 0; i < nodeList.Count; i++)
{
if (nodeList[i].Name == nodeName)
{
return nodeList[i];
}
if (nodeList[i].HasChildNodes)
{
XmlNode node = SearchNode(nodeList[i].ChildNodes, nodeName);
if (node != null)
{
return node;
}
}
}
return null;
}
static XmlNodeList SearchNodeByPath(XmlNodeList nodeList, string xPath)
{
for (int i = 0; i < nodeList.Count; i++)
{
var nodes = nodeList[i].SelectNodes(xPath);
if (nodes != null && nodes.Count > 0)
{
return nodes;
}
if (nodeList[i].HasChildNodes)
{
XmlNodeList innerNodes = SearchNodeByPath(nodeList[i].ChildNodes, xPath);
if (innerNodes != null && innerNodes.Count > 0)
{
return innerNodes;
}
}
}
return null;
}
这是使用方法:
var node = SearchNode(doc.ChildNodes, "compiler");
var node1 = SearchNodeByPath(doc.ChildNodes, "compilers/compiler");
我发现我使用 XPathSelectElement
的解决方案是正确的方法,我只需要在 path/to/key
字符串前加上 //
。
我最终使用的以下代码会执行此操作并去除值外部周围的所有空格(以防值位于与开始标记不同的行上。
// xml is a struct with the path to the parent node (path/to/key)
// and the key name to look up
// Split the parent keys string
XElement elem = doc.Root.XPathSelectElement("//" + xml.KeyPath);
if (elem != null && elem.Element(xml.Key) != null)
replace = elem.Element(xml.Key).Value.Trim();