1

I have tried for hours on this.

I need to access the data in the top section to get the SenderCode

Then I need to step through all of the RecipientDeliveries sections and get the RecipientCode then the name/address info for that one.

To try and get the deliveries I have tried this (along with about 100 variations). No error, but no data is returned ("Enumeration yielded no results")

In the below what I need to do if have code that steps through each of the Recipient sections, grab the 'RecipientCode' for that section and then get the various names and addresses.

I can get the specific SenderCode by doing this:

string xmlText;
            using (var memoryStream = new MemoryStream())
            {
                ASN_Blob.DownloadToStream(memoryStream);
                xmlText = System.Text.Encoding.UTF8.GetString(memoryStream.ToArray());
            }

            var document = XDocument.Parse(xmlText);
            var aw2 = "http://www.omnicare.com/schema/AdvancedShippingNotices.xsd";
            var SenderCode = document.Descendants(XName.Get("SenderCode",aw2)).First().Value;

But keep running into a wall past that.

Here is the XML I need to decode:

<?xml version="1.0" encoding="UTF-8"?>
<ns0:AdvancedShippingNotices xmlns:ns0="http://www.omnicare.com/schema/AdvancedShippingNotices.xsd">
    <ns0:ASNID>4129114</ns0:ASNID>
    <ns0:CourierID>4SAMEDAY</ns0:CourierID>
    <ns0:SenderCode>598</ns0:SenderCode>
    <ns0:SenderNameAndAddress>
        <ns0:Name>Customer of San Diego</ns0:Name>
        <ns0:Address>
            <ns0:Line1>5601 Oberlin Drive, Suite 124</ns0:Line1>
            <ns0:CityTownOrLocality>San Diego</ns0:CityTownOrLocality>
            <ns0:StateOrProvince>CA</ns0:StateOrProvince>
            <ns0:PostalCode>92121-3709</ns0:PostalCode>
        </ns0:Address>
    </ns0:SenderNameAndAddress>
    <ns0:RecipientDeliveries>
        <ns0:Recipient>
            <ns0:RecipientCode>1019</ns0:RecipientCode>
            <ns0:RecipientNameAndAddress>
                <ns0:Name>VILLAGE SQUARE HEALTHCARE CTR</ns0:Name>
                <ns0:Address>
                    <ns0:Line1>1586 W SAN MARCOS BLVD</ns0:Line1>
                    <ns0:CityTownOrLocality>SAN MARCOS</ns0:CityTownOrLocality>
                    <ns0:StateOrProvince>CA</ns0:StateOrProvince>
                    <ns0:PostalCode>92069</ns0:PostalCode>
                </ns0:Address>
            </ns0:RecipientNameAndAddress>
            <ns0:Deliveries>
                <ns0:Delivery>
                    <ns0:DeliveryID>8930798-5</ns0:DeliveryID>
                    <ns0:DeliveryType>ROUTE</ns0:DeliveryType>
                    <ns0:DeliveryRoute>R0130</ns0:DeliveryRoute>
                    <ns0:ToteID>S5-278</ns0:ToteID>
                    <ns0:NursingStation>2</ns0:NursingStation>
                </ns0:Delivery>
            </ns0:Deliveries>
        </ns0:Recipient>
        <ns0:Recipient>
            <ns0:RecipientCode>20366</ns0:RecipientCode>
            <ns0:RecipientNameAndAddress>
                <ns0:Name>OAKMONT OF ESCONDIDO HILLS</ns0:Name>
                <ns0:Address>
                    <ns0:Line1>3012 BEAR VALLEY PKWY</ns0:Line1>
                    <ns0:CityTownOrLocality>ESCONDIDO</ns0:CityTownOrLocality>
                    <ns0:StateOrProvince>CA</ns0:StateOrProvince>
                    <ns0:PostalCode>92025</ns0:PostalCode>
                </ns0:Address>
            </ns0:RecipientNameAndAddress>
            <ns0:Deliveries>
                <ns0:Delivery>
                    <ns0:DeliveryID>8930798-4</ns0:DeliveryID>
                    <ns0:DeliveryType>ROUTE</ns0:DeliveryType>
                    <ns0:DeliveryRoute>R0130</ns0:DeliveryRoute>
                    <ns0:ToteID>F1-101</ns0:ToteID>
                    <ns0:NursingStation>AL</ns0:NursingStation>
                </ns0:Delivery>
            </ns0:Deliveries>
        </ns0:Recipient>
    </ns0:RecipientDeliveries>
</ns0:AdvancedShippingNotices>

UPDATE: There is a good working solution that I accepted. But I also came up with this which was working also. I am posting it here just in case it helps somebody else.

//go get the file pointed to by the message in the Blob Storage
            CloudBlockBlob ASN_Blob = getBlockBlobByName(fileName, storageAccount, blobContainerName);

            string xmlText;
            using (var memoryStream = new MemoryStream())
            {
                ASN_Blob.DownloadToStream(memoryStream);
                xmlText = System.Text.Encoding.UTF8.GetString(memoryStream.ToArray());
            }

 //use a dataset
            DataSet Ds = new DataSet();
            System.IO.StringReader xmlSR1 = new System.IO.StringReader(xmlText);
            Ds.ReadXml(xmlSR1);

 //get all of the individual totes or items
            var Deliveries = (from rec in Ds.Tables["Delivery"].AsEnumerable()

                             select new
                             {
                                 fir = rec[0],
                                 sec = rec[1],
                                 thi = rec[2],
                                 forth = rec[3],
                                 nursingStation = rec[4],
                                 delId = Convert.ToInt16( rec[5])

                             }).ToArray();

//combine them -- needs to be expanded still
var comb3 = (from recipient in Ds.Tables["Recipient"].AsEnumerable()
                         join recName in Ds.Tables["RecipientNameAndAddress"].AsEnumerable() on recipient[1] equals recName[1]
                         join address in Ds.Tables["Address"].AsEnumerable() on recipient[1] equals address[6]
                         select new
                         {
                             recipientName = recName[0],
                             receipientCode = recipient[0],
                             add1 = address[0],
                             Id = recName[1]

                         }
                         ).ToArray();
//prove out that everythying is there
 foreach (var stop in comb3)
            {
                Console.WriteLine($"name: {stop.recipientName} address: {stop.add1}");

                //get all the items for this stop
                var items = (from i in Deliveries where i.delId == Convert.ToInt16(stop.Id) select i).ToArray();
                foreach (var tote in items)
                {
                    Console.WriteLine($"DeliveryId: {tote.fir}  Type:{tote.sec} Nursing Station: -{tote.nursingStation}-");
                }

            }
Joe Ruder
  • 2,122
  • 2
  • 23
  • 52

2 Answers2

0

Try something like this...works on my machine after creating an xml file from your snippet.

XElement dataFromXML = XElement.Parse(xmlText);   
XNamespace aw = "http://www.xxxxxx.com/schema/AdvancedShippingNotices.xsd";
var textSegs = dataFromXML.Descendants(aw + "RecipientDeliveries");
IEnumerable<XElement> textSegs = dataFromXML.Descendants(aw + "RecipientDeliveries");
var recipients = textSegs.Descendants(aw + "RecipientCode");
var address = recipients.Select(x => x.NextNode).FirstOrDefault();

I would also recommend in the future to post a small but valid xml snippet, saves those helping you from having to do so.

Simon Wilson
  • 9,929
  • 3
  • 27
  • 24
  • That does get me a value, but how do I access the various elements inside of it? – Joe Ruder Jul 27 '19 at 15:31
  • Is the edited answer what you are looking for? I think you get the jist, you need a namespace. – Simon Wilson Jul 27 '19 at 15:41
  • The namespace is certainly a step in the right direction....I did not see your edit...checking now. ... – Joe Ruder Jul 27 '19 at 15:44
  • No...I will have to keep banging on it. I see the value returned in address as "WOODLAND CARE CENTER *EMAR*(1)" But I cannot access that value, and it is still only for 1 value vs being able to step through a list of them. I am working on it still. – Joe Ruder Jul 27 '19 at 15:50
0

Using xml linq (XDocument) :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);
            XNamespace ns0 = doc.Root.GetNamespaceOfPrefix("ns0");

            XElement sender = doc.Descendants(ns0 + "SenderNameAndAddress").FirstOrDefault();
            string[] senderAddress = sender.Descendants(ns0 + "Address").Elements().Select(x => (string)x).ToArray();

            XElement recipientDeliveries = doc.Descendants(ns0 + "RecipientDeliveries").FirstOrDefault();

            var results = recipientDeliveries.Elements(ns0 + "Recipient").Select(x => new
            {
                name = (string)x.Descendants(ns0 + "Name").FirstOrDefault(),
                address = x.Descendants(ns0 + "Address").Elements().Select(y => (string)y).ToArray(),
                deliveryID = (string)x.Descendants(ns0 + "DeliveryID").FirstOrDefault(),
                deliveryType = (string)x.Descendants(ns0 + "DeliveryType").FirstOrDefault(),
                deliveryRoute = (string)x.Descendants(ns0 + "DeliveryRoute").FirstOrDefault(),
                toteID = (string)x.Descendants(ns0 + "ToteID").FirstOrDefault(),
                nursingStation = (string)x.Descendants(ns0 + "NursingStation").FirstOrDefault()
            }).ToList();
        }
    }
}
jdweng
  • 33,250
  • 2
  • 15
  • 20
  • I am working on getting this to work for me. I need a way to tie the two arrays together (meaning what deliveries info goes with what addresses) But it is promissing.... – Joe Ruder Jul 27 '19 at 16:06
  • That is super helpful. I have to leave for a short bit but will update everything as soon as I get back. Thank you for the help! – Joe Ruder Jul 27 '19 at 19:01
  • I was in the middle of using data tables and joining them to solve the problem (working but not very clean). This way is much better. I have updated my question with my solution as well, but I am using yours. Thank you . – Joe Ruder Jul 27 '19 at 19:23
  • I have run into a new hurdle, but I opened up a different question if you feel like taking a swing at it @jdweng : https://stackoverflow.com/questions/57236040/breaking-out-all-of-the-elements-in-a-xml-file-c-sharp – Joe Ruder Jul 27 '19 at 21:02