Я пытаюсь создать схему XML с использованием следующего, но у меня возникают проблемы с элементами, которые мы повторяем дважды, и поля не заполняются.
В основном, что происходит, когда программа выходит из sql, получает имена столбцов выбранных таблиц, тогда она должна создать схему, похожую на следующую, не идентично в терминах значений, но да в терминах структуры
public List<TableNames> _tablesNamesList = new List<TableNames>();
public void BuildSchema()
{
foreach (var table in _tablesNamesList)
{
PersistentObject _newObject = new PersistentObject();
List<PersistentObject> _newPersistantObjectList = new List<PersistentObject>();
List<TableDefnition> _newList = db.GetALLTableDeiniations(table.TABLE_NAME);
List<FieldSchemaXml> _newFieldList = new List<FieldSchemaXml>();
foreach (var item in _newList)
{
FieldSchemaXml _newSchema = new FieldSchemaXml();
_newSchema = ConvertSQLDataTypeToSage(item.Type, _newSchema, item);
_newSchema.Name = item.Field;
_newSchema.IsUnique = "false";
_newSchema.IsReadOnly = "false";
_newSchema.IsQueryable = "true";
if (item.is_nullable == 1)
{
_newSchema.IsNullable = "true";
}
else
{
_newSchema.IsNullable = "false";
}
_newSchema.IsReadOnly = "false";
_newSchema.IsUnique = "false";
_newSchema.Group = "false";
_newSchema.IsLockable = "false";
_newSchema.IsDeltaField = "false";
_newSchema.IsPrimaryKey = "false";
_newSchema.FillType = "None";
_newSchema.Direction = "Input";
_newSchema.OverrideFormatting = "false";
_newSchema.ValueSetByDatabase = "false";
_newSchema.FormatScale = "0";
_newSchema.NegativeFormatting = "Standard";
_newSchema.AggregateFunction = "false";
_newSchema.IsExpression = "false";
_newSchema.IsBrowsable = "true";
_newSchema.IsQueryable = "true";
_newSchema.IsEnumeration = "false";
_newSchema.IsAddInPrimaryKey = "false";
_newSchema.IsExcludedFromReset = "false";
_newSchema.IsMember = "false";
_newSchema.AddInTableName = "";
_newObject.IsCacheable = "false";
_newObject.AllowZeroKeys = "false";
_newObject.TransactionMode = "Required";
_newObject.IsStoredProcedure = "false";
_newObject.ProcedureReturnType = "";
_newFieldList.Add(_newSchema);
}
_newPersistantObjectList.Add(_newObject);
_newPObject.PersistentObject.AddRange(_newPersistantObjectList);
}
schemeContent.Text = HelperXml.ToXML(_newPObject);
}
Пример 1.1
<?xml version="1.0" encoding="utf-8"?>
<ObjectStore xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="urn:SageObjectStore">
<ApplicationNamespace>BusinessObjects</ApplicationNamespace>
<MemberVariablePrefix>_</MemberVariablePrefix>
<ClassPrefix>Persistent</ClassPrefix>
<ClassSuffix />
<Language>VB</Language>
<Path>C:\Sage200SchemaExtensions</Path>
<GenerateBusinessObjects>false</GenerateBusinessObjects>
<GenerateSeparateFiles>false</GenerateSeparateFiles>
<PersistentObjects>
<PersistentObject Name="Customer">
<TableName>SLCustomerAccount</TableName>
<Description />
<Fields>
<Field Name="SLCustomerAccountID">
<DbType>Int64</DbType>
<Precision>19</Precision>
<Scale>0</Scale>
<FillType>None</FillType>
<IsNullable>false</IsNullable>
<IsReadOnly>true</IsReadOnly>
<AllowOverwrite>Equal</AllowOverwrite>
<IsPrimaryKey>true</IsPrimaryKey>
<IsDeltaField>false</IsDeltaField>
<IsIndexed>true</IsIndexed>
<IsTransient>false</IsTransient>
<IsUnique>true</IsUnique>
<OverrideFormatting>false</OverrideFormatting>
<IsLockable>false</IsLockable>
<Direction>Input</Direction>
<ValueSetByDatabase>false</ValueSetByDatabase>
<FormatScale>0</FormatScale>
<FormatMask>9999999999999999999</FormatMask>
<NegativeFormatting>Standard</NegativeFormatting>
<Group>false</Group>
<AggregateFunction>None</AggregateFunction>
<IsExcludedFromCopy>false</IsExcludedFromCopy>
<IsExpression>false</IsExpression>
<FriendlyName>SLCustomer Account ID</FriendlyName>
<IsBrowsable>true</IsBrowsable>
<IsQueryable>true</IsQueryable>
<IsEnumeration>false</IsEnumeration>
<IsAddInPrimaryKey>false</IsAddInPrimaryKey>
<AddInTableName />
<AddInRelationField />
<IsMember>false</IsMember>
<IsExcludedFromReset>false</IsExcludedFromReset>
</Field>
</Fields>
<IsCacheable>false</IsCacheable>
<AllowZeroKeys>false</AllowZeroKeys>
<AlwaysAllowPaging>false</AlwaysAllowPaging>
<Namespace>SalesLedger</Namespace>
<PagingFields />
<TransactionMode>Required</TransactionMode>
<IsStoredProcedure>false</IsStoredProcedure>
<ProcedureReturnType />
</PersistentObject>
<PersistentObject Name="MCSSopOrderss">
<TableName />
<Description />
<Fields>
<Field Name="CustomerID">
<DbType>Int64</DbType>
<Precision>11</Precision>
<Scale>0</Scale>
<FillType>None</FillType>
<IsNullable>true</IsNullable>
<IsReadOnly>false</IsReadOnly>
<AllowOverwrite>True</AllowOverwrite>
<IsPrimaryKey>false</IsPrimaryKey>
<IsDeltaField>false</IsDeltaField>
<IsIndexed>false</IsIndexed>
<IsTransient>false</IsTransient>
<IsUnique>false</IsUnique>
<OverrideFormatting>false</OverrideFormatting>
<IsLockable>false</IsLockable>
<Direction>Input</Direction>
<ValueSetByDatabase>false</ValueSetByDatabase>
<FormatScale>0</FormatScale>
<FormatMask>99999999999</FormatMask>
<NegativeFormatting>Standard</NegativeFormatting>
<Group>false</Group>
<AggregateFunction>None</AggregateFunction>
<IsExcludedFromCopy>false</IsExcludedFromCopy>
<IsExpression>false</IsExpression>
<FriendlyName>Customer ID</FriendlyName>
<IsBrowsable>true</IsBrowsable>
<IsQueryable>true</IsQueryable>
<IsEnumeration>false</IsEnumeration>
<IsAddInPrimaryKey>false</IsAddInPrimaryKey>
<AddInTableName />
<AddInRelationField />
<IsMember>false</IsMember>
<IsExcludedFromReset>false</IsExcludedFromReset>
</Field>
<Field Name="TestDate">
<DbType>Date</DbType>
<Precision>3</Precision>
<Scale>0</Scale>
<FillType>None</FillType>
<IsNullable>true</IsNullable>
<IsReadOnly>false</IsReadOnly>
<AllowOverwrite>True</AllowOverwrite>
<IsPrimaryKey>false</IsPrimaryKey>
<IsDeltaField>false</IsDeltaField>
<IsIndexed>false</IsIndexed>
<IsTransient>false</IsTransient>
<IsUnique>false</IsUnique>
<OverrideFormatting>false</OverrideFormatting>
<IsLockable>false</IsLockable>
<Direction>Input</Direction>
<ValueSetByDatabase>false</ValueSetByDatabase>
<FormatScale>0</FormatScale>
<FormatMask>99/99/9999</FormatMask>
<NegativeFormatting>Standard</NegativeFormatting>
<Group>false</Group>
<AggregateFunction>None</AggregateFunction>
<IsExcludedFromCopy>false</IsExcludedFromCopy>
<IsExpression>false</IsExpression>
<FriendlyName>test</FriendlyName>
<IsBrowsable>true</IsBrowsable>
<IsQueryable>true</IsQueryable>
<IsEnumeration>false</IsEnumeration>
<IsAddInPrimaryKey>false</IsAddInPrimaryKey>
<AddInTableName />
<AddInRelationField />
<IsMember>false</IsMember>
<IsExcludedFromReset>false</IsExcludedFromReset>
</Field>
<Field Name="TestString">
<DbType>String</DbType>
<Precision>64</Precision>
<Scale>2</Scale>
<FillType>None</FillType>
<IsNullable>false</IsNullable>
<IsReadOnly>false</IsReadOnly>
<AllowOverwrite>True</AllowOverwrite>
<IsPrimaryKey>false</IsPrimaryKey>
<IsDeltaField>false</IsDeltaField>
<IsIndexed>false</IsIndexed>
<IsTransient>false</IsTransient>
<IsUnique>false</IsUnique>
<OverrideFormatting>false</OverrideFormatting>
<IsLockable>false</IsLockable>
<Direction>Input</Direction>
<ValueSetByDatabase>false</ValueSetByDatabase>
<FormatScale>2</FormatScale>
<NegativeFormatting>Standard</NegativeFormatting>
<Group>false</Group>
<AggregateFunction>None</AggregateFunction>
<IsExcludedFromCopy>false</IsExcludedFromCopy>
<IsExpression>false</IsExpression>
<FriendlyName>Test String</FriendlyName>
<IsBrowsable>true</IsBrowsable>
<IsQueryable>true</IsQueryable>
<IsEnumeration>false</IsEnumeration>
<IsAddInPrimaryKey>false</IsAddInPrimaryKey>
<AddInTableName />
<AddInRelationField />
<IsMember>false</IsMember>
<IsExcludedFromReset>false</IsExcludedFromReset>
</Field>
<Field Name="TestMoney">
<UserDataType>MonetaryValue2dp</UserDataType>
<DbType>String</DbType>
<Precision>12</Precision>
<Scale>2</Scale>
<FillType>None</FillType>
<IsNullable>false</IsNullable>
<IsReadOnly>false</IsReadOnly>
<AllowOverwrite>True</AllowOverwrite>
<IsPrimaryKey>false</IsPrimaryKey>
<IsDeltaField>false</IsDeltaField>
<IsIndexed>false</IsIndexed>
<IsTransient>false</IsTransient>
<IsUnique>false</IsUnique>
<OverrideFormatting>false</OverrideFormatting>
<IsLockable>false</IsLockable>
<Direction>Input</Direction>
<ValueSetByDatabase>false</ValueSetByDatabase>
<FormatScale>2</FormatScale>
<NegativeFormatting>Standard</NegativeFormatting>
<Group>false</Group>
<AggregateFunction>None</AggregateFunction>
<IsExcludedFromCopy>false</IsExcludedFromCopy>
<IsExpression>false</IsExpression>
<FriendlyName>Test Money</FriendlyName>
<IsBrowsable>true</IsBrowsable>
<IsQueryable>true</IsQueryable>
<IsEnumeration>false</IsEnumeration>
<IsAddInPrimaryKey>false</IsAddInPrimaryKey>
<AddInTableName />
<AddInRelationField />
<IsMember>false</IsMember>
<IsExcludedFromReset>false</IsExcludedFromReset>
</Field>
<Field Name="TestDecimal">
<DbType>Decimal</DbType>
<Precision>18</Precision>
<Scale>0</Scale>
<FillType>None</FillType>
<IsNullable>false</IsNullable>
<IsReadOnly>false</IsReadOnly>
<AllowOverwrite>True</AllowOverwrite>
<IsPrimaryKey>false</IsPrimaryKey>
<IsDeltaField>false</IsDeltaField>
<IsIndexed>false</IsIndexed>
<IsTransient>false</IsTransient>
<IsUnique>false</IsUnique>
<OverrideFormatting>false</OverrideFormatting>
<IsLockable>false</IsLockable>
<Direction>Input</Direction>
<ValueSetByDatabase>false</ValueSetByDatabase>
<FormatScale>0</FormatScale>
<FormatMask>999999999999999999.</FormatMask>
<NegativeFormatting>Standard</NegativeFormatting>
<Group>false</Group>
<AggregateFunction>None</AggregateFunction>
<IsExcludedFromCopy>false</IsExcludedFromCopy>
<IsExpression>false</IsExpression>
<FriendlyName>Test Decimal</FriendlyName>
<IsBrowsable>true</IsBrowsable>
<IsQueryable>true</IsQueryable>
<IsEnumeration>false</IsEnumeration>
<IsAddInPrimaryKey>false</IsAddInPrimaryKey>
<AddInTableName />
<AddInRelationField />
<IsMember>false</IsMember>
<IsExcludedFromReset>false</IsExcludedFromReset>
</Field>
<Field Name="TestCurrency">
<DbType>Currency</DbType>
<Precision>11</Precision>
<Scale>2</Scale>
<FillType>None</FillType>
<IsNullable>false</IsNullable>
<IsReadOnly>false</IsReadOnly>
<AllowOverwrite>True</AllowOverwrite>
<IsPrimaryKey>false</IsPrimaryKey>
<IsDeltaField>false</IsDeltaField>
<IsIndexed>false</IsIndexed>
<IsTransient>false</IsTransient>
<IsUnique>false</IsUnique>
<OverrideFormatting>false</OverrideFormatting>
<IsLockable>false</IsLockable>
<Direction>Input</Direction>
<ValueSetByDatabase>false</ValueSetByDatabase>
<FormatScale>2</FormatScale>
<FormatMask>99999999999.99</FormatMask>
<NegativeFormatting>Standard</NegativeFormatting>
<Group>false</Group>
<AggregateFunction>None</AggregateFunction>
<IsExcludedFromCopy>false</IsExcludedFromCopy>
<IsExpression>false</IsExpression>
<FriendlyName>Test Currency</FriendlyName>
<IsBrowsable>true</IsBrowsable>
<IsQueryable>true</IsQueryable>
<IsEnumeration>false</IsEnumeration>
<IsAddInPrimaryKey>false</IsAddInPrimaryKey>
<AddInTableName />
<AddInRelationField />
<IsMember>false</IsMember>
<IsExcludedFromReset>false</IsExcludedFromReset>
</Field>
<Field Name="Test">
<DbType>String</DbType>
<Precision>64</Precision>
<Scale>2</Scale>
<FillType>None</FillType>
<IsNullable>false</IsNullable>
<IsReadOnly>false</IsReadOnly>
<AllowOverwrite>True</AllowOverwrite>
<IsPrimaryKey>false</IsPrimaryKey>
<IsDeltaField>false</IsDeltaField>
<IsIndexed>false</IsIndexed>
<IsTransient>false</IsTransient>
<IsUnique>false</IsUnique>
<OverrideFormatting>false</OverrideFormatting>
<IsLockable>false</IsLockable>
<Direction>Input</Direction>
<ValueSetByDatabase>false</ValueSetByDatabase>
<FormatScale>2</FormatScale>
<NegativeFormatting>Standard</NegativeFormatting>
<Group>false</Group>
<AggregateFunction>None</AggregateFunction>
<IsExcludedFromCopy>false</IsExcludedFromCopy>
<IsExpression>false</IsExpression>
<FriendlyName>Test</FriendlyName>
<IsBrowsable>true</IsBrowsable>
<IsQueryable>true</IsQueryable>
<IsEnumeration>false</IsEnumeration>
<IsAddInPrimaryKey>false</IsAddInPrimaryKey>
<AddInTableName />
<AddInRelationField />
<IsMember>false</IsMember>
<IsExcludedFromReset>false</IsExcludedFromReset>
</Field>
</Fields>
<IsCacheable>false</IsCacheable>
<AllowZeroKeys>false</AllowZeroKeys>
<AlwaysAllowPaging>false</AlwaysAllowPaging>
<Namespace />
<PagingFields />
<TransactionMode>Required</TransactionMode>
<IsStoredProcedure>false</IsStoredProcedure>
<ProcedureReturnType />
</PersistentObject>
</PersistentObjects>
<Relationships>
<Relationship>
<ParentObject>Customer</ParentObject>
<ParentKey>SLCustomerAccountID</ParentKey>
<ChildObject>MCSSopOrderss</ChildObject>
<ChildKey>CustomerID</ChildKey>
<InsertOperation>CheckParentExists</InsertOperation>
<DeleteOperation>CheckForAnyChildren</DeleteOperation>
<IsOneToMany>true</IsOneToMany>
</Relationship>
</Relationships>
<DataTypes />
<Enumerations />
<BaselineSchema>C:\Program Files (x86)\Sage 200
SDK\SageObjectStore.xml</BaselineSchema>
</ObjectStore>
Однако я получаю следующее: для этой таблицы в списке полей есть 44 объекта, поэтому для полей она должна печатать это, и, как вы можете видеть, это игнорирует свойство name, которое я также устанавливаю для сохраняемого объекта.
<?xml version="1.0" encoding="utf-16"?>
<ObjectStore xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<PersistentObjects>
<PersistentObject>
<Fields />
<IsCacheable>false</IsCacheable>
<AllowZeroKeys>false</AllowZeroKeys>
<TransactionMode>Required</TransactionMode>
<ProcedureReturnType />
<IsStoredProcedure>false</IsStoredProcedure>
</PersistentObject>
</PersistentObjects>
<PersistentObject>
<PersistentObject>
<Fields />
<IsCacheable>false</IsCacheable>
<AllowZeroKeys>false</AllowZeroKeys>
<TransactionMode>Required</TransactionMode>
<ProcedureReturnType />
<IsStoredProcedure>false</IsStoredProcedure>
</PersistentObject>
</PersistentObject>
</ObjectStore>
Мой класс для вышеупомянутого.
public class SageXmlDefiniation
{
public class ObjectStore
{
public List<PersistentObject> PersistentObjects = new
List<PersistentObject>();
public string ApplicationnameSpace { get; set; }
public string MemberVariablePrefix { get; set; }
public string ClassPrefix { get; set; }
public string ClassSuffix { get; set; }
public string Language { get; set; }
public string Path { get; set; }
public string GenerateBusinessObjects { get; set; }
public string GenerateSeparateFiles { get; set; }
public List<PersistentObject> PersistentObject
{
get { return PersistentObjects; }
set { PersistentObjects = value; }
}
}
public class PersistentObject
{
private List<FieldSchemaXml> _Fields = new List<FieldSchemaXml>();
[XmlAttribute("Name")]
public string Name { get; set; }
public string TableName { get; set; }
public string Description { get; set; }
[XmlArray("Fields")]
[XmlArrayItem("Field")]
public List<FieldSchemaXml> FieldsList
{
get { return _Fields; }
set { _Fields = value; }
}
public string IsCacheable { get; set; }
public string AllowZeroKeys { get; set; }
public string NameSapce { get; set; }
public string PagingFields { get; set; }
public string TransactionMode { get; set; }
public string ProcedureReturnType { get; set; }
public string IsStoredProcedure { get; set; }
}
public class FieldSchemaXml
{
[XmlAttribute("Name")]
public string Name { get; set; }
public string DBType { get; set; }
public string Precision { get; set; }
public string Scale { get; set; }
public string FillType { get; set; }
public string IsNullable { get; set; }
public string IsReadOnly { get; set; }
public string AllowOverwrite { get; set; }
public string IsPrimaryKey { get; set; }
public string IsDeltaField { get; set; }
public string IsIndexed { get; set; }
public string IsTransient { get; set; }
public string IsUnique { get; set; }
public string OverrideFormatting { get; set; }
public string IsLockable { get; set; }
public string Direction { get; set; }
public string ValueSetByDatabase { get; set; }
public string FormatScale { get; set; }
public string FormatMask { get; set; }
public string NegativeFormatting { get; set; }
public string Group { get; set; }
public string AggregateFunction { get; set; }
public string IsExcludedFromCopy { get; set; }
public string IsExpression { get; set; }
public string FriendlyName { get; set; }
public string IsBrowsable { get; set; }
public string IsQueryable { get; set; }
public string IsEnumeration { get; set; }
public string IsAddInPrimaryKey { get; set; }
public string AddInTableName { get; set; }
public string AddInRelationField { get; set; }
public string IsMember { get; set; }
public string IsExcludedFromReset { get; set; }
}
}
Мой код, который возвращает правильные 44 поля для таблицы выше.
public List<TableDefnition> GetALLTableDeiniations(string tableName)
{
string sql = @"SELECT c.name Field,
t.name Type,
c.max_length MaxLength,
c.Precision,
c.Scale,
c.is_nullable,
c.collation_name
FROM sys.columns c
INNER JOIN sys.types t ON t.system_type_id = c.system_type_id
WHERE object_id = object_id('"+ tableName + "') ORDER BY column_id";
using (var connection = new
sqlConnection(ConfigurationManager.AppSettings["ConnectionString"]))
{
List<TableDefnition> _tableDefnitions = connection.Query<TableDefnition>(sql).ToList();
return _tableDefnitions;
}
}