Here is my xml:

<Instrument RecordCount="3" >
    <Department id = 18384, Sequence=1>
    <InstrumentData StatusCode="1" RoleType="ED" Style="X" DataOther='Data'>
</Department>
<Department id = 18465, Sequence=2>
     <InstrumentData StatusCode="2" RoleType="CD" Style="S" DataOther='Data'>
</Department>
<Department id = 16473, Sequence=3>
    <InstrumentData StatusCode="1" RoleType="CD" Style="T" DataOther='Data'>
</Department>
 </Instrument>

I want @Status attribute ='1' or '2' and not @RoleType='E' and 'F' and @Style ='S' and 'T' for each node.

I have the following statement, but it does not bring back the correct results.

XmlNodeList nodeList = root.SelectNodes(@"//Department[InstrumentData/@Status='1' or Department[InstrumentData/@Status='1' and not (Department[InstrumentData/@RoleType='E' or Department[InstrumentData/@RoleType='F') and (Department[InstrumentData/@Style='S' or Department[InstrumentData/@Style='T') ]", manager);

Or do I first need to get the first condition, then build xml doc, then get next condition.

Thanks.

有帮助吗?

解决方案 2

Managed to get it to work:

XmlNodeList nodeList0 = root.SelectNodes(@"//ns1:Department[(ns1:InstrumentData/@StatusCode='1'
                                                                or ns1:InstrumentData/@StatusCode='2')
                                                                 and not (ns1:InstrumentData/@RoleType='ED' 
                                                                    or ns1:InstrumentData/@RoleType='FD') 
                                                                and (ns1:InstrumentData/@Style='S' 
                                                                     or ns1:InstrumentData/@Style='T') ]", manager);

Thanks for the feedback and prompt responses and input!!!

其他提示

There is no problem to have complex conditions in xpaht expressions. But your example can not work because of some mistakes.
* Some brackets (]) are missing
* There is no Status attribute in your example xml.
* You cant use "or" to put together a note list.

Example: If you try to get Departments with InstrumentData/@StatusCode = 2 and Departments with InstrumentData/@Style= T.

The following will not work:

nodeList = root.SelectNodes("//Department[InstrumentData/@StatusCode='2'] or //Department[InstrumentData/@Style='T' ]");

But you can do either:

nodeList = root.SelectNodes("//Department[InstrumentData/@StatusCode='2'] | //Department[InstrumentData/@Style='T' ]");

Or (in my view better):

nodeList = root.SelectNodes("//Department[InstrumentData/@StatusCode='2' or InstrumentData/@Style='T' ]");
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top