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

Recommended Answers

All 5 Replies

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

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?

Could you not just use teh XMLWriter class? Example

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.

It should be rspxml instead of doc.

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

Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.