locked
XML traversing C# RRS feed

  • Question

  • User640374800 posted

    I need to do validation on an XML file, these cannot be done within the schema easily, it is things like if element A has a certain value, check element B sub elements do not exist or have no value, or checking that an element's sub elements all have unique values (ie no multiple instances of the same value).

    I was wondering what is the most efficient way to traverse and read element values using C#?

    Wednesday, January 20, 2021 8:05 AM

All replies

  • Wednesday, January 20, 2021 8:54 AM
  • User640374800 posted

    Thanks Mike,

    thats very useful, i havent worked with XML in a good few years, but this seems a good way to traverse XML. I have drilled down to get specific values of elements. ie looping through each PayeePayPerWeek

    var type = payeepayperweek.Descendants().Where(n => n.Name.LocalName == "PayeeType").FirstOrDefault().Value;

    I have a question about something i cannot seem to get to work, based on the sample XML here, i would like to count the number of elements where PayeeType was equal to "example type1" and "example type2" so in this case return 2. There can be multiple PayeePayPerWeek elements.

    Also i need to check there are no multiple elements of PayeeType with the same value. So my thinking was to say count the number of element types of PayeePayPerWeek (3 in this case) and compare to a count of PayeePayPerWeek on distinct values for PayeeType? Would this be correct and would you know the syntax to get these counts?

    <n1:PayDetails>
    			<n1:BasicPayPerWeek>100</n1:BasicPayPerWeek>
    		<n1:PayeePayPerWeek>
                <n1:PayeeType>example type1</n1:PayeeType>
                <n1:PayeeRate>1.9</n1:PayeeRate>
                <n1:PayeeRateStatus>example status1</n1:PayeeRateStatus>
            </n1:PayeePayPerWeek>
            <n1:PayeePayPerWeek>
                <n1:PayeeType>example type2</n1:PayeeType>
                <n1:PayeeRate>5</n1:PayeeRate>
                <n1:PayeeRateStatus>example status2/n1:PayeeRateStatus>
            </n1:PayeePayPerWeek>
            <n1:PayeePayPerWeek>
                <n1:PayeeType>example type3</n1:PayeeType>
                <n1:PayeeRate>4</n1:PayeeRate>
                <n1:PayeeRateStatus>example status3</n1:PayeeRateStatus>
            </n1:PayeePayPerWeek>			
    		</n1:PayDetails>

    Wednesday, January 20, 2021 1:31 PM
  • User640374800 posted

    Hi Mike

    Im also trying to retrieve a PayeePayPerWeek element based on the content of a sub element ie PayeeType but all i get back is the  PayeeType element.

    var nodes = emp.Descendants().Where(n => n.Name.LocalName == "PayeeType").Where(x => x.Value == "example type1" || x.Value == "example type2").ToList();
               


    i also tried descendentsandself but that still only bought back the PayeeType NOT PayeePayPerWeek

    Wednesday, January 20, 2021 4:54 PM
  • User-939850651 posted

    Hi mark1961,

    Im also trying to retrieve a PayeePayPerWeek element based on the content of a sub element ie PayeeType but all i get back is the  PayeeType element.

    var nodes = emp.Descendants().Where(n => n.Name.LocalName == "PayeeType").Where(x => x.Value == "example type1" || x.Value == "example type2").ToList();
               
    
    
    

    i also tried descendentsandself but that still only bought back the PayeeType NOT PayeePayPerWeek

    According to the xml structure you provided, the PayeePayPerWeek node is the parent node of the PayeeType node.

    If you need to get the detailed information of the parent node based on the value of the child node, have you tried something like:

    var nodes = emp.Descendants()
                    .Where(n => n.Name.LocalName == "PayeeType")
                    .Where(x => x.Value == "example type1" || x.Value == "example type2")
                    .Select(x => x.Parent)
                    .ToList();

    Hope this can help you.

    Best regards,

    Xudong Peng

    Wednesday, January 27, 2021 9:02 AM