为什么此XPath表达式对一个节点有效,而对另一节点无效?

时间:2019-04-16 06:44:06

标签: c# xml xpath

我有一个函数可以检索XML节点中属性的schedule->command('command Name') ->cron( \Carbon\Carbon::parse( $collection->expires_at )->format( 'i h d m' ) . ' *' );

UUID.randomUUID().toString().substring(0, 4)

.InnerText在静态 string getPropertyFromNode_string(XmlNode node, string propertyName) { try { string selectString = "./empty:content/m:properties/d:"; return node.SelectSingleNode(selectString + propertyName, Utils.nmREST).InnerText; } catch (Exception exception) { Console.WriteLine(exception.Message); throw exception; } } 类的构造函数中定义,如下所示:

nmREST

我在此XmlNode上测试该功能:

Utils

使用此函数调用:

    public static XmlNamespaceManager nmREST = new XmlNamespaceManager(new NameTable());

    static Utils()
    {
        nmREST.AddNamespace("d", "http://schemas.microsoft.com/ado/2007/08/dataservices");
        nmREST.AddNamespace("m", "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata");
        nmREST.AddNamespace("empty", "http://www.w3.org/2005/Atom");
        nmREST.AddNamespace("z", "#RowsetSchema");
    }

,并且值<entry m:etag="&quot;81&quot;" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom"> <id>Web/Lists(guid'someguid')/Items(1213)</id> <category term="SP.Data.LibItem" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" /> <link rel="edit" href="Web/Lists(guid'someguid')/Items(1213)" /> <title /> <updated>2019-04-16T06:16:50Z</updated> <author> <name /> </author> <content type="application/xml"> <m:properties> <d:Id m:type="Edm.Int32" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices">1213</d:Id> <d:FileLeafRef xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices">myfile.xlsm</d:FileLeafRef> <d:FeatureCount m:type="Edm.Double" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices">33</d:FeatureCount> <d:Status xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices">Production Ready</d:Status> <d:CheckoutUserId m:null="true" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" /> <d:EditorId m:type="Edm.Int32" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices">25</d:EditorId> <d:ID m:type="Edm.Int32" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices">1213</d:ID> </m:properties> </content> </entry> 已成功检索。

但是,当我在以下XmlNode上对其进行测试时:

getPropertyFromNode_string(thisNode,"ID")

使用函数调用:

1213

然后<entry m:etag="&quot;24&quot;" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom"> <id>Web/Lists(guid'someguid')/Items(1422)</id> <category term="SP.Data.LibItem" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" /> <link rel="edit" href="Web/Lists(guid'someguid')/Items(1422)" /> <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Station" type="application/atom+xml;type=entry" title="Station" href="Web/Lists(guid'someguid')/Items(1422)/Station"> <m:inline> <entry> <id>anotherguid</id> <category term="SP.Data.DifferentLibItem" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" /> <title /> <updated>2019-04-16T05:58:17Z</updated> <author> <name /> </author> <content type="application/xml"> <m:properties> <d:FacilityNumber xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices">1068</d:FacilityNumber> </m:properties> </content> </entry> </m:inline> </link> <title /> <updated>2019-04-16T05:58:17Z</updated> <author> <name /> </author> <content type="application/xml"> <m:properties> <d:FileLeafRef xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices">thatfilename.xlsm</d:FileLeafRef> <d:Title m:null="true" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" /> </m:properties> </content> </entry> 调用将引发以下消息异常:

getPropertyFromNode_string(thisNode,"FacilityNumber")

我想这意味着XPath表达式无法成功定位SelectSingleNode()元素,因此没有对象可以获取Object reference not set to an instance of an object. 。为什么找不到该元素?第二个节点的XML结构有什么不同,我应该使用什么XPath表达式代替?

1 个答案:

答案 0 :(得分:0)

As per Laurent Lequenne's comment on the OP, including the full nesting sequence of child nodes in the XPath expression successfully retrieves the desired child node, as follows:

./empty:link/m:inline/empty:entry/empty:content/m:properties/d: