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"))
{
}
}
}