0

I'm trying to code this in C# so it will output the values for each element in XML.

Sample XML:

    <APIVersion>4.0</APIVersion> <PackageTrackingInfo> <TrackingNumber>123456789</TrackingNumber> <PackageDestinationLocation> <City>Seattle</City> <StateProvince>WA</StateProvince> <PostalCode>98107</PostalCode> <CountryCode>US</CountryCode> </PackageDestinationLocation> <PackageDeliveryDate> <ScheduledDeliveryDate>2004-09-15</ScheduledDeliveryDate> <ReScheduledDeliveryDate>2004-09-18</ReScheduledDeliveryDate> </PackageDeliveryDate> <TrackingEventHistory> <TrackingEventDetail> <EventStatus>LK</EventStatus> <EventReason>AQ</EventReason> <EventDateTime>2004-08-22T11:00:00-
    08:00</EventDateTime> <EventLocation> <City>SEATTLE</City> <StateProvince>WA</StateProvince> <PostalCode>98107</PostalCode> <CountryCode>US</CountryCode> </EventLocation> <SignedForByName>JOHN GALT</SignedForByName> </TrackingEventDetail>  </PackageTrackingInfo>

Here is what I have so far. What would be the best way to handle outputting the XML.

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

Edited by mldardy

3
Contributors
5
Replies
28
Views
3 Weeks
Discussion Span
Last Post by mldardy
0

Are you trying to read an xml file and display it? Or, take some data and create an xml file representing that data?

0

OK what I'm trying to do is to create a routine in C# 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. 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?

Edited by mldardy

0

Well your code doesn't make a lot of sense. You're adding the Xelements to an object called rspxml, but you're looping through an object called doc, all without defining what those objects are.

0

It should be rspxml instead of doc.

var els = rspxml.Root.Elements("AmazonTrackingResponse").FirstOrDefault().Elements("TrackingEventHistory").FirstOrDefault().Elements("TrackingEventDetail");

Thanks

Edited by mldardy

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.