开发者

c# XML parsing into a GeoCoordinate

I have this XML (example file):

 <Record counter="1">
      <Fields>
       <Field name="id">4234</Field>
       <Field name="county"></Field>
     </Fields>          
     <Distance>
       <Miles>0.14</Miles>
       <KM>0.22</KM>
     </Distance>
     <Point>
       <Lat>51.498199</Lat>
       <Lon>-0.126334</Lon>
     </Point>
</Record>

I have this code below to place it all in the 'vList' collection which is all fine. But, I need to convert the lat and long in a GeoCoordinate.

doing something like this:

GeoCoordinate Location = new GeoCoordinate(Convert.ToDouble(Lat), Convert.ToDouble(Lon));

And it needs to be exposed so it can be seen by the xaml page to convert into Pushpins. It needs to stay in the same collection as vList so all elements of the XML are kept together. I hope this makes sense. I ve spent all day trying to solve this but can't see how to eventually add this Geolocation 'Location' value to my collection. Thanks.

 public void ReadXML()
    {

        var sf = Application.GetResourceStream(new Uri("data.xml", UriKind.Relative));
     using (XmlReader xr = XmlReader.Create(sf.Stream))
     {
         XDocument doc = XDocument.Load(xr);
         var qn = XName.Get("Fields", "http://blahblah/");
         var record = XName.Get("Record", "http://blahblah");

         var Records = from c in doc.Descendants(record)
                      select c;
         ObservableCollection<Venue> vList = new ObservableCollection<Venue>();
         foreach (var x in Records)
         {
             var Fields = from c in x.Descendants(qn)
                          select c;

             Venue lv = new Venue();

             foreach (var t in Fields)
             {
                 foreach (var e in t.Elements())
                 {
                     lv.SaveData(e.Attribute("name").Value, e.Value, lv);
                 }
             }

             var dist = XName.Get("Distance", "http//blahblah");
             var Distance = from c in x.Descendants(dist)
                            select c;
             foreach (var d in Distance)
             {
                 foreach (var e in d.Elements())
                 {
                     lv.SaveData(e.Name.LocalName, e.Value, lv);
                 }
             }

             var pts = XName.Get("Point", "http://blahblah");
             var Point = from c in x.Descendants(pts)
                          select c;

             foreach (var d in Point)
             {
                 f开发者_运维知识库oreach (var e in d.Elements())
                 {
                     lv.SaveData(e.Name.LocalName, e.Value, lv);
                 }
             }

             vList.Add(lv);
             lv = null;
         }
         listBox1.ItemsSource = vList;

          mapItems.Items.Add(vList);
          //or
          mapItems.ItemsSource = vList;
     }

    }

    public class Venue //: INotifyPropertyChanged
    {
        public string id {get;set;}
        public string county { get; set; }
        public string Miles { get; set; }
        public string KM { get; set; }
        public string Lat { get; set; }
        public string Lon { get; set; }

        public void SaveData(string field, string value, Venue v)
        {
            foreach (MemberInfo mi in v.GetType().GetMembers())
            {              
                if (mi.MemberType == MemberTypes.Property)
                {
                    PropertyInfo pi = mi as PropertyInfo;
                    if (pi.Name == field)
                    {
                        pi.SetValue(v, value,null);
                    }
                }
            }
        }

    }


I think you need a type of GeoCoordinate in you Venue model. Don't use the separate properties for Latitude and Longitude. As far as I know the pushpins in XAML only have a Location property where you can bind on, which is expecting a type of GeoCoordinate.

Also I'm not really getting the purpose of the SaveData method. Why would you use reflection when you can just use the properties, which you are already publicly exposing?

I would suggest get rid of the SaveData method with the reflection part, add a GeoCoordinate property and create your objects by using the properties.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜