在 ESRI ArcGIS 中将要素类序列化为 XML

发布于 2024-08-31 06:45:06 字数 128 浏览 2 评论 0原文

如何将 IFeatureClass 对象序列化为 XML?

有一些资源可以在其他 ArcObject 上使用 IXMLSerializer,但这不适用于 IFeatureClass,因为它没有实现 ISerializable。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

神爱温柔 2024-09-07 06:45:06

我实际上已经找到了这个问题的答案。我在这里发布这个问题和答案是为了其他人的利益以及对我的方法的反馈/批评。

IFeatureClass 不能直接序列化,但 IRecordSet2 可以。因此第一步是实现将 IFeatureClass 转换为 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;
}

然后很容易使用 IXMLSerializer 获取 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);           
}

但是,当转换为 IRecordSet2 时,您会丢失要素类名称,因此在写入文件时,我添加一个元素到 XML 中保存要素类名称:

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);
}

现在,只需逆向将 XML 读入要素类的过程即可。请记住,添加了一个元素来存储要素类名称:

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

    return RetreiveFromXml(recordset, sName);
}

使用 IXMLSerializer 进行简单反序列化以获取 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);
}

这是棘手的部分。我愿意接受有关如何改进此问题的建议...将 IRecordSet2 对象隐藏到 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;
}

最后,一个用于删除现有数据集的实用方法。这是从某处复制/粘贴的,但我不记得来源了。

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();
    }
}

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();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文