想要读出xml文件中的地图,上代码看好了:
config.xml:
<?xml version="1.0" encoding="utf-8"?> <Config> <!--图形服务--> <Layers> <Layer ID="2" Type="ArcGISDynamicMapServiceLayer" Url="http://192.168.20.23/ArcGIS/rest/services/WLNZYDK/MapServer" /> <Layer ID="3" Type="ArcGISDynamicMapServiceLayer" Url="http://192.168.20.23/ArcGIS/rest/services/WLGDDK/MapServer"/> </Layers>
</Config>
MainPage.xaml:
<esri:Map x:Name="Map" />
MainPage.xaml.cs:
public void OnLoadConfigComplete() { if (LoadConfigComplete != null) LoadConfigComplete(); } public XDocument ConfigDoc { get; set; } private List<ZHPlatform.Layer.LayerItem> layerItems; public List<LayerItem> LayerItems { get { return layerItems; } set { layerItems = value; } } public MainPage() { InitializeComponent(); //读xml string sourceUrl = Application.Current.Host.Source.ToString(); int removeIndex = sourceUrl.IndexOf("ClientBin"); string configUrl = sourceUrl.Remove(removeIndex, sourceUrl.Length - removeIndex); Uri configUri = new Uri(configUrl); Uri url = new Uri(configUri + "ClientBin/Config.xml?" + Guid.NewGuid().ToString()); WebClient webClient = new WebClient(); webClient.OpenReadAsync(url); webClient.OpenReadCompleted += ConfigLoadCompleted; } void ConfigLoadCompleted(object sender, OpenReadCompletedEventArgs e) { Stream stream = e.Result; using (XmlReader xReader = XmlReader.Create(stream)) { XDocument document = XDocument.Load(xReader); ConfigDoc = document; //读取图层列表 var layers = from f in document.Descendants("Layer") select new ZHPlatform.Layer.LayerItem { ID = f.Attribute("ID").Value, Url = f.Attribute("Url").Value, Type = (LayerType)Enum.Parse(typeof(LayerType), f.Attribute("Type").Value, false), }; if (layerItems == null) layerItems = new List<LayerItem>(); layerItems.AddRange(layers); MyList.ItemsSource = layerItems; Map.Layers = new ESRI.ArcGIS.Client.LayerCollection(); //遍历txt中的layer添加到地图中 foreach (LayerItem litem in layerItems) { ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer dynamicServiceLayer = new ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer(); dynamicServiceLayer.Url = litem.Url; dynamicServiceLayer.ID = litem.ID; Map.Layers.Add(dynamicServiceLayer); } OnLoadConfigComplete(); } }
LayerItem.cs
public class LayerItem { /// <summary> /// 图层ID /// </summary> public string ID { get; set; } private string m_Url = string.Empty; /// <summary> /// 图层地址 /// </summary> public string Url { get { m_Url = WebUtil.ReDirectDynamicUrl(m_Url, string.Empty); return m_Url; } set { m_Url = value; } } }