English 中文(简体)
缩略语
原标题:Serialize a FeatureClass to XML in ESRI ArcGIS

我怎么能够把国际化联会反对XML。

还有一些资源用于将IXMLSerializer用于其他ArcoObjects,但是,由于FeatureClass用于实施国际宇宙科学应用,因此赢得了该图人的工作。

最佳回答

我实际找到了我自己对这个问题的答复。 我在此提出这个问题并作答是为了其他人的利益,也是为了对我的做法提出反馈/批评。

简称表 不能直接分类,但IRecordSet2可能。 因此,第一步是采用一种方法,将国际地联会地图册转换为国际RecordSet:

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获得IRecord Set2:

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

这是骗局。 I m欢迎就如何改进这一......提出建议,将IRecordSet2物体列入国际地联会:

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();
    }
}
问题回答

暂无回答




相关问题
Script or automate feature class creation in ESRI/ArcSDE

I m looking for info on how to write SQL scripts to automate the creation of a versioned feature class in ArcSDE I want to be able to automate the process itself as well as put the scripts under ...

Esri mapobects in Delphi 2010

Has anyone installed ESRI mapobjects activeX controls in Delphi 2010? I get a conflict on tTable as a component name.

Alter map layer symbology dynamically (ArcGIS Server)

What is the preferred method of altering a layer s symbology dynamically? My web app consumes a map service via the REST API, but I don t mind using the SOAP API or ArcObjects (.NET).

热门标签