SelectSingleNode获取属性值

时间:2014-06-17 16:31:04

标签: c# xml

我正在尝试使用select single节点从与特定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 Sample,

<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>

当我运行它时,我得到的Object引用未设置为对象的实例。当我调试时,我看到我的值被设置为null。

我只想快速查询使用XPath从XML中提取值

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

  

1 hit

     

Parameter

     

- @Value: *=127.0.0*

     

- @name: ORBPreferredInterfaces

2 个答案:

答案 0 :(得分:1)

您的xml文件具有默认命名空间xmlns="event_collection/WinCollect",因此您不能仅使用节点的本地名称来获取它们。我会和Linq一起去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;

答案 1 :(得分:1)

你有一个默认的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']