private void GetAttributesFromSelected() { GraphicsLayer selectedLayer = MapApplication.Current.SelectedLayer as GraphicsLayer; IEnumerable<Graphic> selectedGraphics = selectedLayer.SelectedGraphics; int numberOfRows = selectedGraphics.Count(); if (numberOfRows > 0) { string[] columnNames = GetColumnNames(selectedGraphics.First()); string[] attributeArray; List<string[]> dataList = new List<string[]>(); int attributeCounter = 0; foreach (Graphic g in selectedGraphics) // Iterates through chosen objects { attributeArray = new string[25]; ICollection<object> valueCollection = g.Attributes.Values; foreach (object obj in valueCollection) // Iterates through attribute values { if (obj != null) attributeArray[attributeCounter] = obj.ToString(); attributeCounter++; } attributeCounter = 0; dataList.Add(attributeArray); } WriteToCSV(columnNames, dataList); } else MessageBox.Show("No rows exported."); } // Gets column names private string[] GetColumnNames(Graphic graphic) { string[] columnNames = new string[graphic.Attributes.Keys.Count]; int columnIndex = 0; foreach (string s in graphic.Attributes.Keys) { columnNames[columnIndex] = s.ToString(); columnIndex++; } return columnNames; } // Creates the CSV private void WriteToCSV(string[] columnNames, List<string[]> dataList) { SaveFileDialog sfd = new SaveFileDialog(); sfd.Filter = "CSV File|*.csv"; if (sfd.ShowDialog() == true) { using (Stream stream = sfd.OpenFile()) { StreamWriter sw = new StreamWriter(stream, System.Text.Encoding.UTF8); foreach (string s in columnNames) sw.Write(s + ","); //Skriver ut alla kolumnnamn sw.WriteLine(); foreach (string[] strArr in dataList) { foreach (string str in strArr) { sw.Write(str + ","); } sw.WriteLine(); } sw.Close(); stream.Close(); } } } public bool CanExecute(object parameter) { return MapApplication.Current.SelectedLayer != null && MapApplication.Current.SelectedLayer is GraphicsLayer && (MapApplication.Current.SelectedLayer as GraphicsLayer).SelectedGraphics.Count() > 0; }
I made my own if this is to any help. Not the prettiest code and not sure how effective it is with large amounts of data, but it works for my purposes.private void GetAttributesFromSelected() { GraphicsLayer selectedLayer = MapApplication.Current.SelectedLayer as GraphicsLayer; IEnumerable<Graphic> selectedGraphics = selectedLayer.SelectedGraphics; int numberOfRows = selectedGraphics.Count(); if (numberOfRows > 0) { string[] columnNames = GetColumnNames(selectedGraphics.First()); string[] attributeArray; List<string[]> dataList = new List<string[]>(); int attributeCounter = 0; foreach (Graphic g in selectedGraphics) // Iterates through chosen objects { attributeArray = new string[25]; ICollection<object> valueCollection = g.Attributes.Values; foreach (object obj in valueCollection) // Iterates through attribute values { if (obj != null) attributeArray[attributeCounter] = obj.ToString(); attributeCounter++; } attributeCounter = 0; dataList.Add(attributeArray); } WriteToCSV(columnNames, dataList); } else MessageBox.Show("No rows exported."); } // Gets column names private string[] GetColumnNames(Graphic graphic) { string[] columnNames = new string[graphic.Attributes.Keys.Count]; int columnIndex = 0; foreach (string s in graphic.Attributes.Keys) { columnNames[columnIndex] = s.ToString(); columnIndex++; } return columnNames; } // Creates the CSV private void WriteToCSV(string[] columnNames, List<string[]> dataList) { SaveFileDialog sfd = new SaveFileDialog(); sfd.Filter = "CSV File|*.csv"; if (sfd.ShowDialog() == true) { using (Stream stream = sfd.OpenFile()) { StreamWriter sw = new StreamWriter(stream, System.Text.Encoding.UTF8); foreach (string s in columnNames) sw.Write(s + ","); //Skriver ut alla kolumnnamn sw.WriteLine(); foreach (string[] strArr in dataList) // Itererar igenom listans arrayer { foreach (string str in strArr) // Itererar igenom arrayens strängar { sw.Write(str + ","); } sw.WriteLine(); } sw.Close(); stream.Close(); } } } public bool CanExecute(object parameter) { return MapApplication.Current.SelectedLayer != null && MapApplication.Current.SelectedLayer is GraphicsLayer && (MapApplication.Current.SelectedLayer as GraphicsLayer).SelectedGraphics.Count() > 0; }
Did you create this as a add-in? If so, I would be interested in trying this out. Basically, I want to select features in the web application, then export the selected records as a .csv or .dbf.
Hi Ian,
Yes, we've seen this issue before and it is caused by having a record with a field with no value. The workaround is to ensure each field has a value/data.
See this previous post: http://forums.arcgis.com/threads/54576-CSV-Export-Issue
Hope that helps,
Katy