XML Schema使用结束标记和默认值根名称发送空标记

问题描述 投票:0回答:1

我有以下xml我希望重现,但到目前为止,我已经管理以下以获得以下但它应该打印出空元素以及根元素下面是我到目前为止的几个例子。

我使用dapper进行数据访问,使用linq管理我的集合,使用helper类进行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

c# xml xml-serialization dapper
1个回答
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; }

    }
© www.soinside.com 2019 - 2024. All rights reserved.