本文整理汇总了C#中Element.SetEntity方法的典型用法代码示例。如果您正苦于以下问题:C# Element.SetEntity方法的具体用法?C# Element.SetEntity怎么用?C# Element.SetEntity使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Element
的用法示例。
在下文中一共展示了Element.SetEntity方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CreateSetAndExport
/// <summary>
/// Creates a new sample Schema, creates an instance of that Schema (an Entity) in the given element,
/// sets data on that element's entity, and exports the schema to a given XML file.
/// </summary>
/// <returns>A new SchemaWrapper</returns>
public static SchemaWrapperTools.SchemaWrapper CreateSetAndExport(Element storageElement, string xmlPathOut, Guid schemaId, AccessLevel readAccess, AccessLevel writeAccess, string vendorId, string applicationId, string name, string documentation, SampleSchemaComplexity schemaComplexity)
{
#region Start a new transaction, and create a new Schema
if (Schema.Lookup(schemaId) != null)
{
throw new Exception("A Schema with this Guid already exists in this document -- another one cannot be created.");
}
Transaction storageWrite = new Transaction(storageElement.Document, "storageWrite");
storageWrite.Start();
//Create a new schema.
SchemaWrapperTools.SchemaWrapper mySchemaWrapper = SchemaWrapperTools.SchemaWrapper.NewSchema(schemaId, readAccess, writeAccess, vendorId, applicationId, name, documentation);
mySchemaWrapper.SetXmlPath(xmlPathOut);
#endregion
Entity storageElementEntityWrite = null;
//Create some sample schema fields. There are two sample schemas hard coded here, "simple" and "complex."
switch (schemaComplexity)
{
case SampleSchemaComplexity.SimpleExample:
SimpleSchemaAndData(mySchemaWrapper, out storageElementEntityWrite);
break;
case SampleSchemaComplexity.ComplexExample:
ComplexSchemaAndData(mySchemaWrapper, storageElement, xmlPathOut, schemaId, readAccess, writeAccess, vendorId, applicationId, name, documentation, out storageElementEntityWrite);
break;
}
#region Store the main entity in an element, save the Serializeable SchemaWrapper to xml, and finish the transaction
storageElement.SetEntity(storageElementEntityWrite);
TransactionStatus storageResult = storageWrite.Commit();
if (storageResult != TransactionStatus.Committed)
{
throw new Exception("Error storing Schema. Transaction status: " + storageResult.ToString());
}
else
{
mySchemaWrapper.ToXml(xmlPathOut);
return mySchemaWrapper;
}
#endregion
}
示例2: EditExistingData
/// <summary>
/// Create a SchemaWrapper from a Schema Guid and try to find an Entity of a matching Guid
/// in a given Element. If successfull, try to change the data in that Entity.
/// </summary>
/// <param name="storageElement"></param>
/// <param name="schemaId"></param>
/// <param name="schemaWrapper"></param>
public static void EditExistingData(Element storageElement, Guid schemaId, out SchemaWrapperTools.SchemaWrapper schemaWrapper)
{
//Try to find the schema in the active document.
Schema schemaLookup = Schema.Lookup(schemaId);
if (schemaLookup == null)
{
throw new Exception("Schema not found in current document.");
}
//Create a SchemaWrapper.
schemaWrapper = SchemaWrapperTools.SchemaWrapper.FromSchema(schemaLookup);
//Try to get an Entity of the given Schema
Entity storageElementEntityWrite = storageElement.GetEntity(schemaLookup);
if (storageElementEntityWrite.SchemaGUID != schemaId)
{
throw new Exception("SchemaID of found entity does not match the SchemaID passed to GetEntity.");
}
if (storageElementEntityWrite == null)
{
throw new Exception("Entity of given Schema not found.");
}
//Get the fields of the schema
Field fieldInt0 = schemaWrapper.GetSchema().GetField(int0Name);
Field fieldShort0 = schemaWrapper.GetSchema().GetField(short0Name);
Field fieldDouble0 = schemaWrapper.GetSchema().GetField(double0Name);
Field fieldFloat0 = schemaWrapper.GetSchema().GetField(float0Name);
Field fieldBool0 = schemaWrapper.GetSchema().GetField(bool0Name);
Field fieldString0 = schemaWrapper.GetSchema().GetField(string0Name);
//Edit the fields.
Transaction tStore = new Transaction(storageElement.Document, "tStore");
tStore.Start();
storageElementEntityWrite = null;
storageElementEntityWrite = new Entity(schemaWrapper.GetSchema());
storageElementEntityWrite.Set<int>(fieldInt0, 10);
storageElementEntityWrite.Set<short>(fieldShort0, 20);
storageElementEntityWrite.Set<double>(fieldDouble0, 14.2, DisplayUnitType.DUT_METERS);
storageElementEntityWrite.Set<float>(fieldFloat0, 6.12f, DisplayUnitType.DUT_METERS);
storageElementEntityWrite.Set(fieldBool0, true);
storageElementEntityWrite.Set(fieldString0, "goodbye");
//Set the entity back into the storage element.
storageElement.SetEntity(storageElementEntityWrite);
tStore.Commit();
}