Iterate through this XML document

I’m trying to iterate through this XML document in C# .NET and need some assistance. Here is the XML

<APIVersion>4.0</APIVersion>
  <PackageTrackingInfo>
    <TrackingNumber>123456789</TrackingNumber>
    <PackageDestinationLocation>
      <City>Tokyo</City>
      <PostalCode>120-0001</PostalCode>
      <CountryCode>JP</CountryCode>
    </PackageDestinationLocation>
    <PackageDeliveryDate>
      <ScheduledDeliveryDate>2004-09-15</ScheduledDeliveryDate>
      <ReScheduledDeliveryDate>2004-09-18</ReScheduledDeliveryDate>
    </PackageDeliveryDate>
    <TrackingEventHistory>
      <TrackingEventDetail>
        <EventStatus>D1</EventStatus>
        <EventReason>NS</EventReason>
        <EventDateTime>
          2004-08-
          24T11:00:00+09:00
        </EventDateTime>
        <EventLocation>
          <City>TOKYO</City>
          <PostalCode>121-0001</PostalCode>
          <CountryCode>JP</CountryCode>
        </EventLocation>
        <AdditionalLocationInfo>PATIO</AdditionalLocationInfo>
        <SignedForByName>ISHIRO</SignedForByName>
      </TrackingEventDetail>
      <TrackingEventDetail>
        <EventStatus>OD</EventStatus>
        <EventReason>NS</EventReason>
        <EventDateTime>
          2001-08-
          24T07:00:11+09:00
        </EventDateTime>
        <EventLocation>
          <City>TOKYO</City>
          <PostalCode>121-0001</PostalCode>
          <CountryCode>JP</CountryCode>
        </EventLocation>
       
        <EstimatedDeliveryDate>
          2004-08-
          24
        </EstimatedDeliveryDate>
        <DeliveryAppointmentWindow>
          <Day>2004-08-24</Day>
          <StartTime>10:00</StartTime>
          <EndTime>12:00</EndTime>
          <TimeZone>+09:00</TimeZone>
        </DeliveryAppointmentWindow>
      </TrackingEventDetail>        
    </TrackingEventHistory>
  </PackageTrackingInfo>
</AmazonTrackingResponse>

I have this so far but not sure if it is right:

 rspxml.Root.Add(
                    new XElement("API", "4.0"),
                    new XElement("PackageTrackingInfo",
                        new XElement("TrackingNumber", prc.ProNumber)
                    ),
                    new XElement("PackageDestinationLocation",
                        new XElement("City", prc.Consignee.City),
                        new XElement("StateProvince", prc.Consignee.State),
                        new XElement("PostalCode", prc.Consignee.Zipcode),
                        new XElement("CountryCode", prc.Consignee)
                    ),
                    new XElement("PackageDeliveryDate",
                        new XElement("ScheduledDeliveryDate", prc.Consignee),
                        new XElement("ReScheduledDeliveryDate", prc.Consignee)
                    ));
                    var els = doc.Root.Elements("AmazonTrackingResponse").FirstOrDefault().Elements("TrackingEventHistory").FirstOrDefault().Elements("TrackingEventDetail");

                    foreach (XElement row in els) //each row
                    {
                        foreach (XElement col in row.Elements("EventStatus"))
                        {
                            new XElement("EventStatus", EventCode.Delivered.ToXml());
                        }
                        foreach (XElement col in row.Elements("EventReason"))
                        {
                        }
                        foreach (XElement col in row.Elements("EventDateTime"))
                        {
                        }
                        foreach (XElement col in row.Elements("EventLocation"))
                        {
                            foreach (XElement subcol in row.Elements("City"))
                            {
                            }
                            foreach (XElement subcol in row.Elements("PostalCode"))
                            {
                            }
                            foreach (XElement subcol in row.Elements("CountryCode"))
                            {
                            }
                        }
                        foreach (XElement col in row.Elements("EstimatedDeliveryDate"))
                        {
                        }
                        foreach (XElement col in row.Elements("DeliveryAppointmentWindow"))
                        {
                            foreach (XElement subcol in row.Elements("Day"))
                            {
                            }
                            foreach (XElement subcol in row.Elements("StartTime"))
                            {
                            }
                            foreach (XElement subcol in row.Elements("EndTime"))
                            {
                            }
                            foreach (XElement subcol in row.Elements("TimeZone"))
                            {
                            }
                        }
                    }

Can any assist me with this. Please.

Thanks

Trying to figure out why I’m not getting any responses. Is this just too hard to figure out or not able to understand what I’m asking help for?

What exactly are you trying to do, as that XML does not look valid, it doesn’t have a root node. Actually the XML looks incomplete.

OK what I’m trying to do is to create a routine that writes out the XML. The XML is in my original post. I put the routine or method of what I have so far in my original post as well. That is the C# code. THe root is I didn’t post it because it is too long. It has urls and other stuff but that is the root. I just need assistance in a) knowing if what I’m doing in C# is on the right path and if not b) what should I do?

How far is it getting? Is any part of your output working?

Are you sure you want to build the output versus serializing an object into the expected output?

I have a project I did a couple of years ago that outputs XML. I’ll take a look at it to see which approach I took years ago. It might give you some pointers.

OK whatever you have will definitely help. And yes I have to do it this way. This is for Amazon Web Services. I have to pass these values thru my code toAmazon.

You just have to pass it XML right? How the XML gets generated can vary.

I’ll post something tomorrow when I’m back in the office.

Yes I have to pass it as XML.

Thanks I appreciate your help.

Does Amazon provide an XSD for their XML structure? If so, what I did in my application, is used the XSD to generate a class.

These are the notes I documented when generating the class from the XSD

Install XSD2Code Gen (https://xsd2code.codeplex.com)
Right click on XSD, choose Run Xsd2Code generation
	- Behavior
		- EnableInitializeFeeds to False
	- GenericBaseClass
	 - GenerateBaseClass to False
	- PropertyParams
		- EnableLazyLoading to True
	- Serialization 
		- Enabled to True
		- EnableEncoding to True
		- GenerateXmlAttributes to True
	- Collection
		- CollectionObjectType to List

From there on, you interact with the class and at the very end you call .Serialize() on the class when you want to produce your XML content.

Going this route is far easier than mucking with XDocument and XElement and getting thing just right, as your class already holds the correct structure. If Amazon doesn’t have an XSD, then things get a bit more complicated.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.