save layer to xml file

5726
15
07-22-2010 12:13 AM
LedonneJuan
New Contributor
hi, i'm looking for a layer to xml file and load xml file as graphic layer but found nothing. somebody help me please.

thanks
0 Kudos
15 Replies
DominiqueBroux
Esri Frequent Contributor
I've attached C# code allowing XML serialization/deserialization of Graphics.

It's developed as an extension of GraphicsLayer:

So, in WPF, you can serialize to a file with code like:
 
string dataFile = @"graphics.xml";
using (XmlWriter writer = XmlWriter.Create(dataFile, new XmlWriterSettings() { Indent = true }))
{
  myGraphicsLayer.SerializeGraphics(writer);
  writer.Close();
}


and deserialize with :
 
myGraphicsLayer.ClearGraphics();
using (XmlReader reader = XmlReader.Create(dataFile))
{
  myGraphicsLayer.DeserializeGraphics(reader);
  reader.Close();
}


The generated XML looks like:

 
<?xml version="1.0" encoding="utf-8"?>
<Graphics xmlns:sys="http://www.w3.org/2001/XMLSchema" xmlns:esri="http://schemas.datacontract.org/2004/07/ESRI.ArcGIS.Client.Geometry" xmlns:col="http://schemas.datacontract.org/2004/07/System.Collections.Generic" xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/ESRI.ArcGIS.Client.Samples">
<Graphic>
<Attributes>
<Attribute>
<col:key>OBJECTID</col:key>
<col:value i:type="sys:int">1</col:value>
</Attribute>
<Attribute>
<col:key>Name</col:key>
<col:value i:type="sys:string">Graphic1</col:value>
</Attribute>
</Attributes>
<Geometry i:type="esri:Polygon">
<esri:spatialReference>
<esri:wkid>4326</esri:wkid>
</esri:spatialReference>
<esri:rings>
<esri:points>
<esri:point>
<esri:spatialReference i:nil="true" />
<esri:x>120.039</esri:x>
<esri:y>-20.303</esri:y>
</esri:point>
<esri:point>
<esri:spatialReference i:nil="true" />
<esri:x>142.539</esri:x>
<esri:y>-7.0137</esri:y>
</esri:point>
<esri:point>
<esri:spatialReference i:nil="true" />
<esri:x>163.281</esri:x>
<esri:y>-13.923</esri:y>
</esri:point>
<esri:point>
<esri:spatialReference i:nil="true" />
<esri:x>172.773</esri:x>
<esri:y>-35.174</esri:y>
</esri:point>
<esri:point>
<esri:spatialReference i:nil="true" />
<esri:x>173.594</esri:x>
<esri:y>-43.18</esri:y>
</esri:point>
<esri:point>
<esri:spatialReference i:nil="true" />
<esri:x>121.797</esri:x>
<esri:y>-36.032</esri:y>
</esri:point>
<esri:point>
<esri:spatialReference i:nil="true" />
<esri:x>120.039</esri:x>
<esri:y>-20.303</esri:y>
</esri:point>
</esri:points>
</esri:rings>
</Geometry>
</Graphic>
<Graphic>
<Attributes>
<Attribute>
<col:key>OBJECTID</col:key>
<col:value i:type="sys:int">2</col:value>
</Attribute>
<Attribute>
<col:key>Name</col:key>
<col:value i:type="sys:string">Graphic2</col:value>
</Attribute>
</Attributes>
<Geometry i:type="esri:Polygon">
<esri:spatialReference i:nil="true" />
<esri:rings>
<esri:points>
<esri:point>
<esri:spatialReference i:nil="true" />
<esri:x>130.039</esri:x>
<esri:y>-20.303</esri:y>
</esri:point>
<esri:point>
<esri:spatialReference i:nil="true" />
<esri:x>152.539</esri:x>
<esri:y>-7.0137</esri:y>
</esri:point>
<esri:point>
<esri:spatialReference i:nil="true" />
<esri:x>173.281</esri:x>
<esri:y>-13.923</esri:y>
</esri:point>
<esri:point>
<esri:spatialReference i:nil="true" />
<esri:x>179.773</esri:x>
<esri:y>-35.174</esri:y>
</esri:point>
<esri:point>
<esri:spatialReference i:nil="true" />
<esri:x>179.594</esri:x>
<esri:y>-43.18</esri:y>
</esri:point>
<esri:point>
<esri:spatialReference i:nil="true" />
<esri:x>131.797</esri:x>
<esri:y>-36.032</esri:y>
</esri:point>
<esri:point>
<esri:spatialReference i:nil="true" />
<esri:x>130.039</esri:x>
<esri:y>-20.303</esri:y>
</esri:point>
</esri:points>
</esri:rings>
</Geometry>
</Graphic>
</Graphics>


You can also use it with other kind of stream. Example in silverlight with IsolatedStorage:
 
string dataFile = @"graphics.xml";
using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream fileStream = store.CreateFile(dataFile))
{
using (XmlWriter writer = XmlWriter.Create(fileStream, new XmlWriterSettings() { Indent = true }))
{
graphicsLayer.SerializeGraphics(writer);
writer.Close();
}
fileStream.Close();
}�??


Note that the symbol is not serialized, so it's only working if there is a renderer associted to your layer or if you reset the symbols by code after the deserialization (else the grahics end up wihout any symbol and so are not rendered.
LedonneJuan
New Contributor
Thanks a lot Dominique. That's the one I need.

God Bless 🙂
0 Kudos
XavierGuillard1
New Contributor
Hi , I'm developing a MapControl Application using ARGIS Engine 9.3 .I'm wondering if it possible to serialize like you do, the IGraphicLayer or IGraphicContainer From ESRI.Arcgis.Carto to XML and also to deseralize it. I think the process is very similar to yours .

Somebody can help me please.

Thanks in advance .
0 Kudos
BKuiper
Occasional Contributor III
I've attached C# code allowing XML serialization/deserialization of Graphics.

It's developed as an extension of GraphicsLayer:

So, in WPF, you can serialize to a file with code like:


Great post, thanks. I started out with the XmlSerializer but switch to the DataContractSerializer because of the support within the ESRI. I also used IXmlSerializer which is a interface to overwrite the regular Xml Serialization parsing and insert your own. This helped me because i made some derived classes from GraphicsLayer and Graphics that I didn't want to change that much.

Thanks again for the great post. Hopefully the ESRI objects will be further extended so we don't need to hack our own anymore 🙂
0 Kudos
VishakhaKadam
New Contributor
Hi Dominique,

First of all I would like to thank you for this post. Great post!!

I have a question about it. I am using both the classes DataContracts.cs and GraphicsLayerExtension.cs.
    GraphicsLayer glayer = layer as GraphicsLayer;
                        using (XmlWriter writer = XmlWriter.Create(xmlFile+dataFile, new XmlWriterSettings() { Indent = true }))
                        {
                            glayer.SerializeGraphics(writer);
                            writer.Close();
                        }
This works fine. Please find attached xml file. However when when I use DeserilaizeGraphics method it only identifies  <Geometry i:type="esri:Polygon">.

Here is my code:

  ESRI.ArcGIS.Client.GraphicsLayer gfxLyr = new ESRI.ArcGIS.Client.GraphicsLayer();
   using (XmlReader testReader = XmlReader.Create(new FileStream(@"C:\Users\vkadam\Documents\CommandCore\SaveData\graphicsTest.xml", FileMode.Open)))
                        {
                            gfxLyr.DeserializeGraphics(testReader);
                            gfxLyr.ID = "test";
                            testReader.Close();

                            cont.BaseMap.Layers.Add(gfxLyr);

                            for(int n = 0; n < gfxLyr.Graphics.Count; n++)
                            {
                               Debug.WriteLine(gfxLyr.Graphics.Geometry);
/// does not show   <Geometry i:type="esri:Polyline">
                            }
0 Kudos
VishakhaKadam
New Contributor
Hi Dominique,

First of all I would like to thank you for this post. Great post!!

I have a question about it. I am using both the classes DataContracts.cs and GraphicsLayerExtension.cs.

Here is code for serialization of graphic:

    GraphicsLayer glayer = layer as GraphicsLayer;
                        using (XmlWriter writer = XmlWriter.Create(xmlFile+dataFile, new XmlWriterSettings() { Indent = true }))
                        {
                            glayer.SerializeGraphics(writer);
                            writer.Close();
                        }
This works fine. Please find attached xml file. However when when I use DeserilaizeGraphics method it only identifies  <Geometry i:type="esri:Polygon">.

Here is my code:

  ESRI.ArcGIS.Client.GraphicsLayer gfxLyr = new ESRI.ArcGIS.Client.GraphicsLayer();
   using (XmlReader testReader = XmlReader.Create(new FileStream(@"C:\Users\vkadam\Documents\CommandCore\SaveData\graphicsTest.xml", FileMode.Open)))
                        {
                            gfxLyr.DeserializeGraphics(testReader);
                            gfxLyr.ID = "test";
                            testReader.Close();

                            BaseMap.Layers.Add(gfxLyr);

                            for(int n = 0; n < gfxLyr.Graphics.Count; n++)
                            {
                               Debug.WriteLine(gfxLyr.Graphics.Geometry);
      // does not show   <Geometry i:type="esri:Polyline"> and  <Geometry i:type="esri: point">
                            }

This only shows Polygon type and not <Geometry i:type="esri:Polyline"> and  <Geometry i:type="esri: point>

Am I missing something ? I really appreciate your help !!

Waiting for your reply...

Thank you,

Sincerely,
Vishakha
0 Kudos
DominiqueBroux
Esri Frequent Contributor
I tested your xml file and got 11 graphics including polylines and points.

ESRI.ArcGIS.Client.Geometry.Polygon
ESRI.ArcGIS.Client.Geometry.Polygon
ESRI.ArcGIS.Client.Geometry.Polygon
ESRI.ArcGIS.Client.Geometry.Polygon
ESRI.ArcGIS.Client.Geometry.Polygon
ESRI.ArcGIS.Client.Geometry.Polyline
ESRI.ArcGIS.Client.Geometry.Polyline
ESRI.ArcGIS.Client.Geometry.Polyline
ESRI.ArcGIS.Client.Geometry.Polyline
Point[X=-11370863.6659683, Y=4344881.59797506, WKID=102100]
Point[X=-10566392.7121314, Y=4192007.05534562, WKID=102100]


How may graphics do you get?
0 Kudos
VishakhaKadam
New Contributor
Hi Dominique,

Here is my output:

ESRI.ArcGIS.Client.Geometry.Polygon
ESRI.ArcGIS.Client.Geometry.Polygon
ESRI.ArcGIS.Client.Geometry.Polygon
ESRI.ArcGIS.Client.Geometry.Polygon
ESRI.ArcGIS.Client.Geometry.Polygon

I am still not able to find why it is not showing all the 11 graphics items as you said. I only get the  5 items.

Please find attached class  files which I am using. Please let me know what am I missing or doing incorrect.

Thank you,
Vishakha
0 Kudos
VishakhaKadam
New Contributor
Hi Dominique,

I found out that , it was this code showing only polygon types.

for(int n = 0; n < gfxLyr.Graphics.Count; n++)
{
Debug.WriteLine(gfxLyr.Graphics.Geometry);
// does not show <Geometry i:type="esri:Polyline"> and <Geometry i:type="esri: point">
}

Instead of using for loop, I used  foreach (var graphics in gfxLyr.Graphics) and it worked !!

Thank you so much for your help!!

Sincerely,
Vishakha
0 Kudos