C#针对一个XmlDocument评估多个XPath
我正在编写一个C#程序,其中我希望将一系列XPath语句存储为字符串,并根据XMLDocument(或其他一些C#XML结构,如果有更好的C#XML结构)对它们进行求值,然后将结果值存储在字典/对象中 我的挑战是我的XPath无法被评估 作为一个非常简化的示例,假设这是我的XML:C#针对一个XmlDocument评估多个XPath,c#,xml,xpath,xml-parsing,C#,Xml,Xpath,Xml Parsing,我正在编写一个C#程序,其中我希望将一系列XPath语句存储为字符串,并根据XMLDocument(或其他一些C#XML结构,如果有更好的C#XML结构)对它们进行求值,然后将结果值存储在字典/对象中 我的挑战是我的XPath无法被评估 作为一个非常简化的示例,假设这是我的XML: <root> <a> <child1 Id="Id1" Name="Name1" /> <child2 Id="Id2" Name="
<root>
<a>
<child1 Id="Id1" Name="Name1" />
<child2 Id="Id2" Name="Name2" />
</a>
</root>
(使用Id
attribute=“Id1”)获取a
的子元素的名称)
我试图编写的代码的简化版本是:
var xpath = @"//a/*[@Id='Id1']/name()";
var xml = @"<root><a><child1 Id='Id1' Name='Name1' /><child2 Id='Id2' Name='Name2' /></a></root>";
var doc = new XmlDocument();
doc.LoadXml(xml);
var navigator = doc.CreateNavigator();
string ChildName = (string)navigator.Evaluate(xpath);
var xpath=@”//a/*[@Id='Id1']/name();
var xml=@;
var doc=新的XmlDocument();
doc.LoadXml(xml);
var navigator=doc.CreateNavigator();
string ChildName=(string)navigator.Evaluate(xpath);
但是我得到了一个错误,我的XPath有一个无效的标记-我假设它是name()
部分
有没有办法使用直接XPath语句而不是遍历树来实现这一点
谢谢 如果我理解正确,您只需要重新安排XPath即可。试试这个:
name(//a/*[@Id='Id1'])
非常确定,如果我理解正确,您只需要重新排列XPath。试试这个:
name(//a/*[@Id='Id1'])
使用xml liinq:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication68
{
class Program
{
static void Main(string[] args)
{
string xml = "<root>" +
"<a>" +
"<child1 Id=\"Id1\" Name=\"Name1\" />" +
"<child2 Id=\"Id2\" Name=\"Name2\" />" +
"</a>" +
"</root>";
XDocument doc = XDocument.Parse(xml);
Dictionary<string, string> dict = doc.Descendants("a").FirstOrDefault().Elements().Where(x => x.Name.LocalName.StartsWith("child"))
.GroupBy(x => (string)x.Attribute("Id"), y => (string)y.Attribute("Name"))
.ToDictionary(x => x.Key, y => y.FirstOrDefault());
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Xml;
使用System.Xml.Linq;
命名空间控制台应用程序68
{
班级计划
{
静态void Main(字符串[]参数)
{
字符串xml=“”+
"" +
"" +
"" +
"" +
"";
XDocument doc=XDocument.Parse(xml);
Dictionary dict=doc.subscriptions(“a”).FirstOrDefault().Elements()。其中(x=>x.Name.LocalName.StartsWith(“子”))
.GroupBy(x=>(string)x.Attribute(“Id”),y=>(string)y.Attribute(“Name”))
.ToDictionary(x=>x.Key,y=>y.FirstOrDefault());
}
}
}
使用xml语言:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication68
{
class Program
{
static void Main(string[] args)
{
string xml = "<root>" +
"<a>" +
"<child1 Id=\"Id1\" Name=\"Name1\" />" +
"<child2 Id=\"Id2\" Name=\"Name2\" />" +
"</a>" +
"</root>";
XDocument doc = XDocument.Parse(xml);
Dictionary<string, string> dict = doc.Descendants("a").FirstOrDefault().Elements().Where(x => x.Name.LocalName.StartsWith("child"))
.GroupBy(x => (string)x.Attribute("Id"), y => (string)y.Attribute("Name"))
.ToDictionary(x => x.Key, y => y.FirstOrDefault());
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Xml;
使用System.Xml.Linq;
命名空间控制台应用程序68
{
班级计划
{
静态void Main(字符串[]参数)
{
字符串xml=“”+
"" +
"" +
"" +
"" +
"";
XDocument doc=XDocument.Parse(xml);
Dictionary dict=doc.subscriptions(“a”).FirstOrDefault().Elements()。其中(x=>x.Name.LocalName.StartsWith(“子”))
.GroupBy(x=>(string)x.Attribute(“Id”),y=>(string)y.Attribute(“Name”))
.ToDictionary(x=>x.Key,y=>y.FirstOrDefault());
}
}
}
不幸的是,这需要对每个值进行编码-我正在寻找一种通用解决方案,例如存储文本XPath。你对此有什么想法吗?不过,谢谢你的代码。没有更多的示例数据,我无法给出答案。我试图使尽可能通用,但不知道你所说的“每个值”是什么意思。不幸的是,这需要为每个值编码-我正在寻找一个通用的解决方案,例如存储文本XPath。你对此有什么想法吗?不过,谢谢你的代码。没有更多的示例数据,我无法给出答案。我试着尽可能的泛化,但不知道你所说的“每个值”是什么意思。这很有效!非常感谢。更大的问题是,使用XmlDocument和Navigator的想法是否正确。在这种情况下,您是否认为这是正确的解决方案?@JohnBustos使用XmlDocument和navigator是一种很好的方法,只要您计划只评估像这样的XPath语句。对于大多数.NETXML用途,我通常推荐使用LINQtoXML(您也可以使用XPathSelectElements
方法使用LINQtoXML执行XPath)。如果您希望进行更多的XML操作,我建议您切换到该操作。这很有效!非常感谢。更大的问题是,使用XmlDocument和Navigator的想法是否正确。在这种情况下,您是否认为这是正确的解决方案?@JohnBustos使用XmlDocument和navigator是一种很好的方法,只要您计划只评估像这样的XPath语句。对于大多数.NETXML用途,我通常推荐使用LINQtoXML(您也可以使用XPathSelectElements
方法使用LINQtoXML执行XPath)。如果您希望进行更多的XML操作,我建议您切换到这一点。