-1

I am struggling to find out how to pull out the tracking number(s) values from the following XML:

 <ShipmentHeader>
    <DocumentID>62017836-1</DocumentID>
    <DocumentReference type="CustomerSuppliedReference">
       <DocumentID>
          <ID>10532</ID>
       </DocumentID>
    </DocumentReference>
    <Status>
       <Code/>
       <Description languageID="en-us"/>
    </Status>
    <ActualShipDateTime>2023-06-07T09:16:33-04:00</ActualShipDateTime>
    <ShippingMethod>CHEAPEST METHOD W/FIRST CLASS</ShippingMethod>
    <DocumentReference type="TrackingNumber">
       <TrackingNumber>9200190233107605458375</TrackingNumber>
    </DocumentReference>
 </ShipmentHeader>

From what I have seen, it seems that I must use reflection, but I just cannot get it to work.
Can anyone give me any pointers please?

zx485
  • 28,498
  • 28
  • 50
  • 59
Trevor Daniel
  • 3,785
  • 12
  • 53
  • 89
  • 3
    "From what i have seen it seem i must used reflection" - no, definitely not. I suggest you read a tutorial on LINQ to XML, which is a really powerful and easy-to-use XML API. – Jon Skeet Jun 09 '23 at 13:45
  • Does this answer your question? [How do I read and parse an XML file in C#?](https://stackoverflow.com/questions/642293/how-do-i-read-and-parse-an-xml-file-in-c) – Progman Jun 09 '23 at 14:37

4 Answers4

1

try this code

    var results = XDocument.Parse(xml)
       .Descendants("TrackingNumber")
       .Select(e => e.Value)
       .FirstOrDefault(); // Or .ToList() maybe
Serge
  • 40,935
  • 4
  • 18
  • 45
0

A possibly more performant method than @Serge, still using XDocument

XDocument doc = XDocument.Parse(xml);

var result = 
    doc.Root
       .Elements("DocumentReference")
       .Where(e => (string)e.Attribute("type") == "TrackingNumber")
       .SelectMany(e => e.Elements("TrackingNumber"))
       .Select(e => e.Value);

Or you can use XQuery

var result = doc.XPathSelectElements(
    @"/ShipmentHeader/DocumentReference[@type=""TrackingNumber""]/TrackingNumber")
  .Select(e => e.Value);

dotnetfiddle

Charlieface
  • 52,284
  • 6
  • 19
  • 43
0

As I understand you are trying to find any "TrackingNumber" element, at any level:

void Main()
{
    string s = @"<ShipmentHeader>
            <DocumentID>62017836-1</DocumentID>
            <DocumentReference type=""CustomerSuppliedReference"">
               <DocumentID>
                  <ID>10532</ID>
               </DocumentID>
            </DocumentReference>
            <Status>
               <Code/>
               <Description languageID=""en-us""/>
            </Status>
            <ActualShipDateTime>2023-06-07T09:16:33-04:00</ActualShipDateTime>
            <ShippingMethod>CHEAPEST METHOD W/FIRST CLASS</ShippingMethod>
            <DocumentReference type=""TrackingNumber"">
               <TrackingNumber>9200190233107605458375</TrackingNumber>
            </DocumentReference>
         </ShipmentHeader>";
         
    var numbers = XElement.Parse(s)
        .Descendants("TrackingNumber")
        .Select(xe => (string)xe);
        
    foreach (var number in numbers)
    {
        Console.WriteLine(number);
    }
}
Cetin Basoz
  • 22,495
  • 3
  • 31
  • 39
0

an alternative way;

    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.LoadXml("your xml");

    XmlNodeList trackingNumberNodes = xmlDoc.SelectNodes("//DocumentReference[@type='TrackingNumber']/TrackingNumber");
    foreach (XmlNode node in trackingNumberNodes)
    {
        Console.WriteLine(node.InnerText);
    }

https://dotnetfiddle.net/N5eNno