question

ManoharReddyDigital-4070 avatar image
0 Votes"
ManoharReddyDigital-4070 asked DanielZhang-MSFT commented

Selecting Node values in Array using Xpath

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


dotnet-csharp
image.png (123.2 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

DanielZhang-MSFT avatar image
0 Votes"
DanielZhang-MSFT answered DanielZhang-MSFT commented

Hi @ManoharReddyDigital-4070,
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.



· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks Daniel.in your sample small correction is header to be closed before EcommerceNotification.if i give Descendants("UPsscsc") not getting values


<?xml version="1.0" standalone="yes"?>
<Upsscs SchemaVersion="1.0" applicationVersion="1.0.">
<Header></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>

0 Votes 0 ·
DanielZhang-MSFT avatar image DanielZhang-MSFT ManoharReddyDigital-4070 ·

Hi @ManoharReddyDigital-4070,
The Descendants method is used to return a collection of the descendant elements for this document or element.
So it should start from Header element in my test.
More details please refer to this document.
And please pay attention to spelling and check your xml.
It is Upsscs instead of UPsscsc.
<Header></Header>
Best Regards,
Daniel Zhang




0 Votes 0 ·