IEnumerable<SPAN class="operator token"><</SPAN>Layer<SPAN class="operator token">></SPAN> layers_to_export <SPAN class="operator token">=</SPAN> <SPAN class="keyword token">this</SPAN><SPAN class="punctuation token">.</SPAN>ViewModel<SPAN class="punctuation token">.</SPAN>Map<SPAN class="punctuation token">.</SPAN>OperationalLayers<SPAN class="punctuation token">.</SPAN><SPAN class="token function">Where</SPAN><SPAN class="punctuation token">(</SPAN>I <SPAN class="operator token">=</SPAN><SPAN class="operator token">></SPAN> I<SPAN class="punctuation token">.</SPAN>Name<SPAN class="punctuation token">.</SPAN><SPAN class="token function">Equals</SPAN><SPAN class="punctuation token">(</SPAN>activeLayerName<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="token function">ToList</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN>
<SPAN class="keyword token">foreach</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="keyword token">var</SPAN> layer_to_export <SPAN class="keyword token">in</SPAN> layers_to_export<SPAN class="punctuation token">)</SPAN>
<SPAN class="punctuation token">{</SPAN>
<SPAN class="keyword token">if</SPAN> <SPAN class="punctuation token">(</SPAN>layer_to_export<SPAN class="punctuation token">.</SPAN>Name<SPAN class="punctuation token">.</SPAN><SPAN class="token function">EndsWith</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"kmz"</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">||</SPAN> layer_to_export<SPAN class="punctuation token">.</SPAN>Name<SPAN class="punctuation token">.</SPAN><SPAN class="token function">EndsWith</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"KMZ"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="punctuation token">{</SPAN>
<SPAN class="keyword token">var</SPAN> kml_layer <SPAN class="operator token">=</SPAN> layer_to_export<SPAN class="punctuation token">.</SPAN><SPAN class="token function">Clone</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">as</SPAN> KmlLayer<SPAN class="punctuation token">;</SPAN>
<SPAN class="keyword token">await</SPAN> kml_layer<SPAN class="punctuation token">.</SPAN><SPAN class="token function">LoadAsync</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN>
KmlDocument kmlDoc <SPAN class="operator token">=</SPAN> <SPAN class="keyword token">new</SPAN> <SPAN class="token class-name">KmlDocument</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN>
KmlDataset kmlDataset <SPAN class="operator token">=</SPAN> <SPAN class="keyword token">new</SPAN> <SPAN class="token class-name">KmlDataset</SPAN><SPAN class="punctuation token">(</SPAN>kmlDoc<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN>
<SPAN class="keyword token">foreach</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="keyword token">var</SPAN> node <SPAN class="keyword token">in</SPAN> kml_layer<SPAN class="punctuation token">.</SPAN>Dataset<SPAN class="punctuation token">.</SPAN>RootNodes<SPAN class="punctuation token">)</SPAN>
<SPAN class="punctuation token">{</SPAN>
kmlDoc<SPAN class="punctuation token">.</SPAN>ChildNodes<SPAN class="punctuation token">.</SPAN><SPAN class="token function">Add</SPAN><SPAN class="punctuation token">(</SPAN>node<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN>
<SPAN class="punctuation token">}</SPAN>
<SPAN class="punctuation token">}</SPAN>
<SPAN class="punctuation token">}</SPAN>
<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>When this code is run, an exception is thrown at line 22: "kmlDoc.ChildNodes.Add(node);".
Exception: Esri.ArcGISRuntime.ArcGISRuntimeException: 'Object already owned.: Already owned.'
I have found that a similar exception is thrown (from some google searching) when attempting to add a reference of an object that is already being "used" somewhere else. In this case i presume that the node being Add() to the ChildNodes of the kmlDoc is already owned by the kml_layer object, therefore it cannot be added.
I am going down the path of recreating everything in the kml from scratch via parsing the kml manually. I feel like this could be done with much less code...but i havent been able to figure it out.
Thank you.