在下面的XML中,我使用SelectSingleNode
的XmlDocument
來提取結果值。// @ attrib和// name/@ attrib在C#
evtASxml.SelectSingleNode(@"//@value").Value
返回第一個「值」的值。
evtASxml.SelectSingleNode(@"//Result/@value").Value
引發空例外。
有人能解釋這是怎麼回事?
<Event xmlns="http://schemas.microsoft.com/win/2004/08/events/event">
<System>
<Provider Name="Microsoft-Windows-CAPI2" Guid="{f00f00-f00-f00f00-f00-f00f00f00}" />
<EventID>30</EventID>
<Version>0</Version>
<Level>2</Level>
<Task>30</Task>
<Opcode>0</Opcode>
<Keywords>0x4000000000000001</Keywords>
<TimeCreated SystemTime="2012-04-08T23:43:37.573242200Z" />
<EventRecordID>4828</EventRecordID>
<Correlation ActivityID="{f00f00-f00-f00-f00-f00f00f00f00}" />
<Execution ProcessID="7512" ThreadID="3220" />
<Channel>Microsoft-Windows-CAPI2/Operational</Channel>
<Computer>Matt-Seven</Computer>
<Security UserID="S-f00-f00-f00-f00f00f00-f00f00f00-f00f00f00-f00f00" />
</System>
<UserData>
<CertVerifyCertificateChainPolicy>
<Policy type="CERT_CHAIN_POLICY_SSL" constant="4" />
<Certificate fileRef="f00f00f00f00f00f00f00f00f00f00f00.cer" subjectName="www.example.com" />
<CertificateChain chainRef="{f00f00-f00-f00-f00-f00f00f00f00}" />
<Flags value="0" />
<SSLAdditionalPolicyInfo authType="server" serverName="example.com">
<IgnoreFlags value="0" />
</SSLAdditionalPolicyInfo>
<Status chainIndex="0" elementIndex="0" />
<EventAuxInfo ProcessName="iexplore.exe" />
<CorrelationAuxInfo TaskId="{f00f00-f00-f00-f00-f00f00f00f00}" SeqNumber="4" />
<Result value="800B010F">The certificate's CN name does not match the passed value.</Result>
</CertVerifyCertificateChainPolicy>
</UserData>
</Event>
我的事件日誌中的數字值被替換爲f00。
我相信現在它與命名空間影響XPATH查詢的方式有關。 http://msdn.microsoft.com/en-us/library/ms950779.aspx – Matt 2012-04-09 01:13:20
好的,添加'XmlNamespaceManager ns = new XmlNamespaceManager(evtASxml.NameTable); ns.AddNamespace(「foo」,「http://schemas.microsoft.com/win/2004/08/events/event」);'並使用'evtASxml.SelectSingleNode(@「// foo:Result/@ value」 ,ns).Value'按預期工作。命名空間看起來很不雅觀。 – Matt 2012-04-09 01:39:42