How do I serialize a custom class containing instances of ArcGIS Pro objects?

484
1
01-14-2020 06:42 AM
StevenCorpus1
New Contributor II

What I am trying to do is to take an instance of our custom class that is in a general format as shown below:

 

using System;

using ArcGIS.Core.Geometry;

using ArcGIS.OtherStuff

using SpatialReference = ArcGIS.Core.Geometry.SpatialReference;

               namespace myNameSpace

               {             

                              public class myClass

                              {

                                            

                                             private bool _field1;

                                            

                                             public bool Field1

                                             {

                                                            get {return _field1;}

                                                            set

                                                            {

                                                                           _field1 = value;

                                                            }

                                             }

              

                                             //So SpatialReference of new shapefile needs to come from NewLayerSpatialRef

                                             private SpatialReference _newLayerSpatialRef;

                                            

                                             public SpatialReference NewLayerSpatialRef

                                             {

                                                            get { return _newLayerSpatialRef; }

                                                            set

                                                            {

                                                                           _newLayerSpatialRef = value;

                                                            }

                                             }

                                             ..........//Other ArcGIS Pro classes

                                            

                                             ..........//Other non-ArcGIS Pro classes

                              }

               }

Say I create an instance of this class name myInstance and populate it with all necessary properties.

I would then like to serialize the instance into a file in order to persist it for future use by doing some LIKE the following:

 

var writer = new StreamWriter(@“c:\temp\myFileForFutureUse.xml”);

                    XmlSerializer serializer = new XmlSerializer(typeof(myClass));

                    serializer.Serialize(writer, myInstance);

 

I don’t necessarily need to use the XmlSerializer class.

 

Is something like the above possible? Can I serialize an instance of a class that contains instances of ArcGIS Pro classes such as SpatialReference? If so, is there a particular serialization method suggested by Esri such as Xml Serialization or Binary Serialization? Are there any coding examples or white papers? Any guidance on this matter would be appreciated.

 

Thanks!

Tags (2)
0 Kudos
1 Reply
JohnJones
Esri Contributor

Per this Stack Overflow thread... c# - Implementing Custom XML Serialization/Deserialization of compound data type? - Stack Overflow 

You may want to mark your SpatialReference (& other ArcGIS Pro objects) as 

    [XmlIgnore]
    public SpatialReference NewLayerSpatialRef

and then add a string version along side...

    [XmlAttribute]    public string NewLayerSpatialRefValue { get => NewLayerSpatialRef.ToXML(); }

Deserializtion should be similar, taking the string and calling the appropriate builder type's FromXML static member.  Of course, if you have some non-serializable object; for example a feature, you would want to either serialize a primary key to it (OID), along with a reference to its datasource location, or a snapshot of its current attributes, depending on your needs.

Using this method, your serialized XML will have a string property with another XML fragment inside it rather than a 'nicer' looking direct XML sub-tree merge.  I would imagine there is some way of getting the latter behavior, but I'm not familiar with the .NET serialization libraries to get that effect and it would provide much benefit unless that was the format required by the client parsing the XML.

0 Kudos