Xml与预期结果不符

时间:2019-03-24 17:10:40

标签: c# xml xml-serialization

我正在尝试使用以下内容构建架构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;
 }

}

0 个答案:

没有答案