Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 759785
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:38:19+00:00 2026-05-14T15:38:19+00:00

How can I serialize an IFeatureClass object to XML? There are some resources for

  • 0

How can I serialize an IFeatureClass object to XML?

There are some resources for using IXMLSerializer on other ArcObjects, but that won’t work for IFeatureClass because it doesn’t implement ISerializable.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T15:38:20+00:00Added an answer on May 14, 2026 at 3:38 pm

    I’ve actual found my own answer to this question. I’m posting this question and answer here for the benefit of others and for feedback/critique on my approach.

    IFeatureClass cannot be serialized directly, but IRecordSet2 can be. So the first step is implementing a method to convert IFeatureClass to IRecordSet2:

    private static IRecordSet2 ConvertToRecordset(IFeatureClass fc)
    {
        IRecordSet recSet = new RecordSetClass();
        IRecordSetInit recSetInit = recSet as IRecordSetInit;
        recSetInit.SetSourceTable(fc as ITable, null);
    
        return (IRecordSet2) recSetInit;
    }
    

    Then it’s easy to use IXMLSerializer to get XML:

    public static XElement StoreAsXml(IFeatureClass fc)
    {
        // Can't serialize a feature class directly, so convert
        //  to recordset first.
        IRecordSet2 recordset = ConvertToRecordset(fc);
    
        IXMLSerializer xmlSer = new XMLSerializerClass();
        string sXml = xmlSer.SaveToString(recordset, null, null);
    
        return XElement.Parse(sXml);           
    }
    

    However, when you convert to IRecordSet2, you lose the feature class name, so when writing to a file, I add an element to the XML to hold the feature class name:

    public static void StoreToFile(IFeatureClass fc, string filePath)
    {
        XElement xdoc = StoreAsXml(fc);
    
        XElement el = new XElement("FeatureClass", new XAttribute( "name", fc.AliasName ),
                                    xdoc);
    
        el.Save(filePath);
    }
    

    Now, just reverse the process to read the XML into a feature class. Remember that an element was added to store the feature class name:

    public static IFeatureClass RetreiveFromFile(string filepath)
    {
        XElement xdoc = XElement.Load(filepath);
        string sName = xdoc.FirstAttribute.Value;
        XNode recordset = xdoc.FirstNode;
    
        return RetreiveFromXml(recordset, sName);
    }
    

    Simple de-serialization using IXMLSerializer to get a IRecordSet2:

    public static IFeatureClass RetreiveFromXml(XNode node, string sName)
    {
        IXMLSerializer xmlDeSer = new XMLSerializerClass();
        IRecordSet2 recordset = (IRecordSet2)xmlDeSer.LoadFromString(node.ToString(), null, null);
    
        return ConvertToFeatureClass(recordset, sName);
    }
    

    This was the tricky part. I’m open to suggestions on how to improve this … covert the IRecordSet2 object into an IFeatureClass:

    private static IFeatureClass ConvertToFeatureClass(IRecordSet2 rs, string sName)
    {
        IWorkspaceFactory pWSFact = new ShapefileWorkspaceFactory();
    
        string sTempPath = Path.GetTempPath();
        IFeatureWorkspace pFWS = (IFeatureWorkspace)pWSFact.OpenFromFile( sTempPath, 0);
    
        // Will fail (COM E_FAIL) if the dataset already exists
        DeleteExistingDataset(pFWS, sName);
    
        IFeatureClass pFeatClass = null;
        pFeatClass = pFWS.CreateFeatureClass(sName, rs.Fields, null, null, esriFeatureType.esriFTSimple,
                                             "SHAPE", "");
    
        // Copy incoming record set table to new feature class's table
        ITable table = (ITable) pFeatClass;
        table = rs.Table;
    
        IFeatureClass result = table as IFeatureClass;
    
        // It will probably work OK without this, but it makes the XML match more closely
        IClassSchemaEdit3 schema = result as IClassSchemaEdit3;
        schema.AlterAliasName(sName);
        schema.AlterFieldAliasName("FID", "");
        schema.AlterFieldModelName("FID", "");
        schema.AlterFieldAliasName("Shape", "");
        schema.AlterFieldModelName("Shape", "");
    
        // If individual fields need to be edited, do something like this:
        //      int nFieldIndex = result.Fields.FindField("Shape");
        //      IFieldEdit2 field = (IFieldEdit2)result.Fields.get_Field(nFieldIndex);
    
        // Cleanup 
        DeleteExistingDataset(pFWS, sName);
    
        return table as IFeatureClass;
    }
    

    Finally, a utility method for deleting an existing dataset. This was copy/pasted from somewhere, but I can’t remember the source.

    public static void DeleteExistingDataset(IFeatureWorkspace pFWS, string sDatasetName)
    {
        IWorkspace pWS = (IWorkspace)pFWS;
        IEnumDatasetName pEDSN = pWS.get_DatasetNames(esriDatasetType.esriDTFeatureClass);
        bool bDatasetExists = false;
        pEDSN.Reset();
        IDatasetName pDSN = pEDSN.Next();
        while (pDSN != null)
        {
            if (pDSN.Name == sDatasetName)
            {
                bDatasetExists = true;
                break;
            }
            pDSN = pEDSN.Next();
        }
        if (bDatasetExists)
        {
            IFeatureClass pFC = pFWS.OpenFeatureClass(sDatasetName);
            IDataset pDataset = (IDataset)pFC;
            pDataset.Delete();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I can serialize an object to a file using: var writeStream = File.Open(l.osl, FileMode.Create);
I have an object I can serialize to XML without a problem. However when
I have some data in a C# DataSet object. I can serialize it right
I am able to serialize a single type/class but is there a way that
Is there a jQuery plugin out there that can serialize a form, and then
I am trying to serialize some Linq objects using this code. private byte[] GetSerializedObj(object
I've always wanted to send objects that you can serialize in XML across websites,
I've did some research. I've found that you can serialize then unserialize to get
What C-sharp type can I serialize to get JSON object with format name:[[1,2,3],[1,2,3],[1,2,3]] If
Today, I have read that command's object in WPF can be serialized. And I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.