XML模式发送带有结束标记和默认值根名称的空标记

时间:2019-03-23 19:05:38

标签: c# xml xml-serialization dapper

我有以下要重现的xml,但到目前为止,我已经管理了以下内容以获取以下内容,但它应该打印出空元素以及根元素,以下是到目前为止我所拥有的一些示例

我正在使用dapper进行数据访问,并使用linq来管理集合,并使用了一个帮助程序类来进行xml序列化

<?xml version="1.0" encoding="utf-16"?>
<ArrayOfFieldSchemaXml xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <FieldSchemaXml>
    <Name>LineID</Name>
    <Precision>19</Precision>
    <Scale>0</Scale>
  </FieldSchemaXml>
  <FieldSchemaXml>
    <Name>OrderID</Name>
    <Precision>19</Precision>
    <Scale>0</Scale>
  </FieldSchemaXml>
  <FieldSchemaXml>
    <Name>OrderNumber</Name>
    <Precision>0</Precision>
    <Scale>0</Scale>
  </FieldSchemaXml>
  <FieldSchemaXml>
    <Name>StockItemID</Name>
    <Precision>19</Precision>
    <Scale>0</Scale>
  </FieldSchemaXml>
  <FieldSchemaXml>
    <Name>ItemType</Name>
    <Precision>0</Precision>
    <Scale>0</Scale>
  </FieldSchemaXml>
  <FieldSchemaXml>
    <Name>ItemCode</Name>
    <Precision>0</Precision>
    <Scale>0</Scale>
  </FieldSchemaXml>
  <FieldSchemaXml>
    <Name>ItemDesc</Name>
    <Precision>0</Precision>
    <Scale>0</Scale>
  </FieldSchemaXml>
  <FieldSchemaXml>
    <Name>DepotCode</Name>
    <Precision>0</Precision>
    <Scale>0</Scale>
  </FieldSchemaXml>
  <FieldSchemaXml>
    <Name>Text</Name>
    <Precision>0</Precision>
    <Scale>0</Scale>
  </FieldSchemaXml>
  <FieldSchemaXml>
    <Name>CostPrice</Name>
    <Precision>19</Precision>
    <Scale>4</Scale>
  </FieldSchemaXml>
  <FieldSchemaXml>
    <Name>UnitOfSale</Name>
    <Precision>0</Precision>
    <Scale>0</Scale>
  </FieldSchemaXml>
  <FieldSchemaXml>
    <Name>Price</Name>
    <Precision>19</Precision>
    <Scale>4</Scale>
  </FieldSchemaXml>
  <FieldSchemaXml>
    <Name>LineTotal</Name>
    <Precision>19</Precision>
    <Scale>4</Scale>
  </FieldSchemaXml>
  <FieldSchemaXml>
    <Name>NominalCode</Name>
    <Precision>0</Precision>
    <Scale>0</Scale>
  </FieldSchemaXml>
  <FieldSchemaXml>
    <Name>NominalCC</Name>
    <Precision>0</Precision>
    <Scale>0</Scale>
  </FieldSchemaXml>
  <FieldSchemaXml>
    <Name>NominalDept</Name>
    <Precision>0</Precision>
    <Scale>0</Scale>
  </FieldSchemaXml>
  <FieldSchemaXml>
    <Name>VatCode</Name>
    <Precision>10</Precision>
    <Scale>0</Scale>
  </FieldSchemaXml>
  <FieldSchemaXml>
    <Name>VatCode</Name>
    <Precision>10</Precision>
    <Scale>0</Scale>
  </FieldSchemaXml>
  <FieldSchemaXml>
    <Name>VatRate</Name>
    <Precision>0</Precision>
    <Scale>0</Scale>
  </FieldSchemaXml>
  <FieldSchemaXml>
    <Name>VatRate</Name>
    <Precision>0</Precision>
    <Scale>0</Scale>
  </FieldSchemaXml>

</ArrayOfFieldSchemaXml>

正确的布局上面的我的下面应该是什么样子?这是我创建的工具,可以节省我通过第三方程序创建xml的时间,而我正在自动执行此程序以减少时间。

<PersistentObject Name="SopOrderss">
      <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>

以下是我目前的模型。

public class FieldSchemaXml
{
    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; }
}

以下是我的系列,所以您可以看到我仍然需要生产空项目,并且有一种方法可以给我默认值,例如,如果 在此列表中,false为空,我如何将其设置为false并仍然输出?。

public  static class HelperXml
{
    public static T FromXML<T>(string xml)
    {
        using (StringReader stringReader = new StringReader(xml))
        {
            XmlSerializer serializer = new XmlSerializer(typeof(T));
            return (T)serializer.Deserialize(stringReader);
        }
    }

    public static  string ToXML<T>(T obj)
    {
        using (StringWriter stringWriter = new StringWriter(new StringBuilder()))
        {
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
            xmlSerializer.Serialize(stringWriter, obj);
            return stringWriter.ToString();
        }
    }
}

这是我的“生成”按钮下的代码,该代码正在调用上述toxml方法

 private void genXmlSchema_Click(object sender, EventArgs e)
 {
        List<TableDefnition> _newList= db.GetALLTableDeiniations();
        List<FieldSchemaXml> _newSchemaList = new List<FieldSchemaXml>();

        foreach(var item in _newList)
        {
            FieldSchemaXml _newSchema = new FieldSchemaXml();
            _newSchema.Name = item.Field;
            _newSchema.Precision = item.Precision.ToString();
            _newSchema.Scale = item.Scale.ToString();
            _newSchemaList.Add(_newSchema);
        }

        schemeContent.Text = HelperXml.ToXML(_newSchemaList);
 }

问题。

  1. 如何添加根节点PersistentObject和TableName并以与我想模仿的示例布局相同的方式描述我的模式。

编辑2 当我按照以下人员的建议尝试操作时,在运行时编译时没有出现对象未设置错误的错误,这就是我试图将我的对象传递到列表中的方式。

private void genXmlSchema_Click(object sender, EventArgs e)
{
    string _tableName = "";

    PersistentObject _newObject = new PersistentObject();
    List<FieldSchemaXml> _newFieldList = new List<FieldSchemaXml>();

    foreach (var item in _newList)
    {
        _newObject.TableName = "MCSGROUP";
        _newObject.Name = "mcsgroup";
        FieldSchemaXml _newSchema = new FieldSchemaXml();

        _newSchema.Precision = item.Precision.ToString();
       _newSchema.Scale = Convert.ToString(item.Scale);
        _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";
        _newObject.TableName = "MCSGroupTest";
        _newObject.Name = "test";
        _newSchema.IsLockable = "false";
        _newSchema.IsDeltaField = "false";
        _newSchema.IsPrimaryKey = "false";
        _newSchema.FillType= "None";
        _newSchema.Direction = "Input";

        _newObject.Fields.Add(_newSchema);
    }            
    schemeContent.Text = HelperXml.ToXML(_newObject);
}

根据下面的答案,我的新定义。

 public class SageXmlDefiniation
 {
    public class PersistentObject
    {
        [XmlAttribute("Name")]
        public string Name { get; set; }

        public string TableName { get; set; }
        public string Description { get; set; }
        [XmlArray("Fields")]
        [XmlArrayItem("Field")]
        public List<FieldSchemaXml> Fields { 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; }
    }
}

enter image description here

1 个答案:

答案 0 :(得分:1)

使用以下类:

    public class PersistentObject
    {
        [XmlAttribute("Name")]
        public string Name { get; set; }

        public string TableName { get; set; }
        public string Description { get; set; }
        [XmlArray("Fields")]
        [XmlArrayItem("Field")]
        List<FieldSchemaXml> Fields { 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; }

    }