选择tsinglenode获取属性值

本文关键字:属性 获取 tsinglenode 选择 | 更新日期: 2023-09-27 18:09:05

我试图使用选择单个节点从匹配特定xpath查询的XML返回值。

这是我正在使用的代码,

XmlDocument doc = new XmlDocument();
doc.Load(@"C:'Program Files'Config.xml");
string xPath;

xPath = "/Configuration/Service[@name='InfoRepositoryClient']/Environment/Parameter[@name='ORBPreferredInterfaces']";
string value = doc.SelectSingleNode(xPath).Attributes["value"].Value;
Console.WriteLine("The value is {0}", value);
Console.WriteLine("XML Doc is ...");
XML示例中,

<Configuration version="7.2.0" xmlns="event_collection/WinCollect">
<Service version="7.2.0.799013" classification="Service" type="Service" module="WinCollectCommon" name="RegistryCache">
    <Environment/>
</Service>
<Service version="7.2.0.799013" classification="Static" type="Service" module="AgentCore" name="AgentCore">
    <Environment>
        <Parameter value="300000" name="HeartbeatInterval"/>
        <Parameter value="60000" name="ConfigurationCheckInterval"/>
        <Parameter value="true" name="Enabled"/>
        <Parameter value="false" name="Deleted"/>
    </Environment>
</Service>
<Service version="7.2.0.799013" classification="Service" type="Service" module="WinCollectCommon" name="InfoRepositoryClient">
    <Environment>
        <Parameter value="0" name="DCPSDebugLevel"/>
        <Parameter value="*=127.0.0*" name="ORBPreferredInterfaces"/>
        <Parameter value="44380" name="DomainId"/>
        <Parameter value="corbaloc::127.0.0.1:12345/DCPSInfoRepo" name="DCPSInfoRepo"/>
        <Parameter value="1" name="DCPSBit"/>
    </Environment>
</Service>

当我运行这个时,我得到对象引用没有设置为对象的实例。当我调试时,我看到我的值被设置为null。

我只是想要一个快速的查询,从XML中提取一个值使用XPath

注意,当我在notepad++中运行xpath查询时,我得到以下结果

1 hit

Parameter

- @Value: *=127.0.0*

- @name: ORBPreferredInterfaces

选择tsinglenode获取属性值

您已经得到了一个默认的xmlns,即xmlns="event_collection/WinCollect",继承自根Configuration。您应该使用XmlNameSpaceManager来注册名称空间,然后将其传递给SelectSingleNode调用:

var nsm = new XmlNamespaceManager(doc.NameTable);
nsm.AddNamespace("e", "event_collection/WinCollect");
string value = doc.SelectSingleNode(xPath, nsm).Attributes["value"].Value;

还需要限定xpath:

/e:Configuration/e:Service[@name='InfoRepositoryClient']
/e:Environment/e:Parameter[@name='ORBPreferredInterfaces']

您的xml文件具有默认名称空间xmlns="event_collection/WinCollect",因此您不能仅使用节点的本地名称来获取它们。我会用Linq to Xml

var xdoc = XDocument.Load(@"C:'Program Files'Config.xml");
var ns = xdoc.Root.GetDefaultNamespace();
var parameter = 
    xdoc.Root.Elements(ns + "Service")
        .Where(s => (string)s.Attribute("name") == "InfoRepositoryClient")
        .Elements(ns + "Environment")
        .Elements(ns + "Parameter")
        .Where(p => (string)p.Attribute("name") == "ORBPreferredInterfaces")
        .Select(p => (string)p.Attribute("value"))
        .FirstOrDefault();
输出:

"*=127.0.0*"
与XPath:

XmlNamespaceManager nsmgr = new XmlNamespaceManager(new NameTable());
nsmgr.AddNamespace("ns", "event_collection/WinCollect");
var xpath = "ns:Service[@name='InfoRepositoryClient']/ns:Environment/ns:Parameter[@name='ORBPreferredInterfaces']";
var parameter = xdoc.Root.XPathSelectElement(xpath, nsmgr);
var value = (string)parameter.Attribute("value"); 

使用旧的XmlDocument API:

var doc = new XmlDocument();
doc.Load(@"C:'Program Files'Config.xml");
XmlNamespaceManager nsmgr = new XmlNamespaceManager(new NameTable());
nsmgr.AddNamespace("ns", "event_collection/WinCollect");
var xpath = "ns:Service[@name='InfoRepositoryClient']/ns:Environment/ns:Parameter[@name='ORBPreferredInterfaces']";
var parameter = doc.DocumentElement.SelectSingleNode(xpath, nsmgr); 
string value = parameter.Attributes["value"].Value;