简体   繁体   中英

Using LINQ how would I check if a XML row contains a field and if it does check a different field on the same row for a value

I have a scenario where an application is spitting out some XML to me and I don't have any control of what it's structure is I have posted it below.

<?xml version="1.0"?>
<DATASET xmlns:dt="urn:schemas-microsoft-com:datatypes">
    <ROW> 
        <OccNumber >test</OccNumber>
        <OccId >Test2</OccId>
        <OccTime >2017/01/26 09:38</OccTime>
        <OccSummary >Test worked</OccSummary>
        <DATASET>
            <ROW>
                <PID>123456</PID>
                <CID >12345678</CID>
            </ROW>
            <ROW>
                <PID>569867</PID>
                <CID>37576334</CID>
            </ROW>
        <DATASET>
            <ROW>
                <ReportId >4345454</ReportId>
                <ReportTime >2018/02/15 12:55</ReportTime>
                <NumberType4 />
                <accepted >Yes</accepted>
                <cond1>No </Cond1>
            </ROW>
        </DATASET>
    </ROW>
</DATASET>

So what I need to do is basically count any time I find a tag and with in the same row the tag has a value of "Yes". I would like to do this with LINQ if possible.

EDIT: To be a bit more clear. I am assuming that because the Dataset and Row tag's repeat and don't have a unique name I can't identify a specific one to check for my count. What I do know is that the ACCEPTED and COND1 tags are unique to the data-set and row I am interested in counting. I also need to check the values in both fields as it's a combination of what I find that tells me if I should count the row or not.

Your XML isn't valid (missing closing tag on one of the DATASET elements, different case in the cond1 element). Below is some corrected XML with additional nodes that can be used for testing and some code beneath it showing how you can select the nodes you're interested in.

<?xml version="1.0"?>
<DATASET xmlns:dt="urn:schemas-microsoft-com:datatypes">
    <ROW> 
        <OccNumber >test</OccNumber>
        <OccId >Test2</OccId>
        <OccTime >2017/01/26 09:38</OccTime>
        <OccSummary >Test worked</OccSummary>
       <DATASET>
            <ROW>
                <ReportId >4345454</ReportId>
                <ReportTime >2018/02/15 12:55</ReportTime>
                <NumberType4 />                
                <cond1>No</cond1>
            </ROW>
        </DATASET>      
        <DATASET>
            <ROW>
                <ReportId >4345454</ReportId>
                <ReportTime >2018/02/15 12:55</ReportTime>
                <NumberType4 />
                <accepted>Yes</accepted>
                <cond1>No</cond1>
            </ROW>
        </DATASET>              
        <DATASET>
            <ROW>
                <PID>123456</PID>
                <CID >12345678</CID>
            </ROW>
            <ROW>
                <PID>569867</PID>
                <CID>37576334</CID>
            </ROW>
        </DATASET>
        <DATASET>
            <ROW>
                <ReportId >4345454</ReportId>
                <ReportTime >2018/02/15 12:55</ReportTime>
                <NumberType4 />
                <accepted>Yes</accepted>
                <cond1>No</cond1>
            </ROW>
        </DATASET>
    </ROW>
</DATASET>

Code:

var xdoc = new XmlDocument();
xdoc.LoadXml("YOUR XML STRING HERE");
var nodes = xdoc.SelectNodes("//DATASET/ROW[accepted='Yes' and cond1='No']");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM