Selecting Node values in Array using Xpath

manu 41 Reputation points
2021-06-24T03:26:29.98+00:00

Hi ,We have a requirement to fecth collection of node values into array from XML.we tried multiple expressions using XPath.but always getting null values.Please help how to get expect results in array.

Values to select: /Upsscs/EcommerceNotification/OrderReferenceFields/ReferenceField

Expected Result is Array=[79787709,936N5D4CPU]

108798-image.png

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,648 questions
0 comments No comments
{count} votes

Accepted answer
  1. Daniel Zhang-MSFT 9,621 Reputation points
    2021-06-24T07:12:12.047+00:00

    Hi @manu ,
    According to your xml file, is it caused by the missing node Order?
    I made a test with a simple xml and I can get the values.
    Here is my code example:

    var xml = XElement.Load(@"C:\Users\Desktop\test.xml");  
            IEnumerable<XElement> de =from el in xml.Descendants("Header").Elements("EcommerceNotification").Elements("Order").Elements("OrderReferenceFields").Elements("ReferenceField") select el;  
            foreach (XElement el in de)  
                Console.WriteLine(el.Value);  
    

    And the following is my test.xml:

    <?xml version="1.0" standalone="yes"?>  
    <Upsscs SchemaVersion="1.0" applicationVersion="1.0.">  
    <Header>  
      <EcommerceNotification type="OrderCreatedNotification">  
        <Order number="26875690">  
         <OrderReferenceFields>  
            <ReferenceField id="1">79787709</ReferenceField>  
            <ReferenceField id="2">936N5D4CPU</ReferenceField>  
            <ReferenceField id="3"/>  
            <ReferenceField id="4"/>  
            <ReferenceField id="5"/>  
         </OrderReferenceFields>  
       </Order>  
      </EcommerceNotification>  
    </Header>  
    </Upsscs>  
    

    Here is also a related thread you can refer to.
    If the problem is still not resolved, please provide your code and xml file.
    Best Regards,
    Daniel Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful