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

C# XML模式发送带有结束标记和默认值根名称的空标记,c#,xml,xml-serialization,dapper,C#,Xml,Xml Serialization,Dapper,我有下面的xml我想复制,但到目前为止,我已经管理下面得到以下内容,但它应该打印出空元素以及根元素。下面是我到目前为止的一些示例 我使用dapper进行数据访问,使用linq管理集合,使用helper类进行xml序列化 <?xml version="1.0" encoding="utf-16"?> <ArrayOfFieldSchemaXml xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="htt

我有下面的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>
下面是我的序列化器,你可以看到,我仍然需要生成空项,有没有一种方法可以给出默认值,例如 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();
        }
    }
}
公共静态类HelperXml
{
公共静态T FromXML(字符串xml)
{
使用(StringReader StringReader=new StringReader(xml))
{
XmlSerializer serializer=新的XmlSerializer(typeof(T));
返回(T)序列化程序。反序列化(stringReader);
}
}
公共静态字符串ToXML(T obj)
{
使用(StringWriter StringWriter=new StringWriter(new StringBuilder()))
{
XmlSerializer XmlSerializer=新的XmlSerializer(typeof(T));
Serialize(stringWriter,obj);
返回stringWriter.ToString();
}
}
}
这是我在generate按钮下的代码,该按钮调用上述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);
 }
private void genXmlSchema_单击(对象发送方,事件参数e)
{
List _newList=db.GetALLTableDeiniations();
列表_newSchemaList=新列表();
foreach(新列表中的变量项)
{
FieldSchemaXml_newSchema=新的FieldSchemaXml();
_newSchema.Name=item.Field;
_newSchema.Precision=item.Precision.ToString();
_newSchema.Scale=item.Scale.ToString();
_newSchema.Add(_newSchema);
}
schemeContent.Text=HelperXml.ToXML(_newSchemaList);
}
问题

  • 如何将根节点PersistentObject和TableName以及description添加到模式中,方式与我提供的要模拟的示例布局相同
  • 编辑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);
    }
    
    private void genXmlSchema_单击(对象发送方,事件参数e)
    {
    字符串_tableName=“”;
    PersistentObject_newObject=新PersistentObject();
    列表_newFieldList=新列表();
    foreach(新列表中的变量项)
    {
    _newObject.TableName=“MCSGROUP”;
    _newObject.Name=“mcsgroup”;
    FieldSchemaXml_newSchema=新的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”;
    }否则
    _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=“无”;
    _newSchema.Direction=“输入”;
    _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; }
        }
    }
    
    公共类定义
    {
    公共类持久对象
    {
    [XmlAttribute(“名称”)]
    公共字符串名称{get;set;}
    公共字符串表名{get;set;}
    公共字符串说明{get;set;}
    [XmlArray(“字段”)]
    [XmlArrayItem(“字段”)]
    公共列表字段{get;set;}
    }
    公共类FieldSchemaXml
    {
    [XmlAttribute(“名称”)]
    公共字符串名称{get;set;}
    公共字符串DBType{get;set;}
    公共字符串精度{get;set;}
    公共字符串比例{get;set;}
    公共字符串填充类型{get;set;}
    公共字符串可为空{get;set;}
    公共字符串IsReadOnly{get;set;}
    公共字符串AllowOverwrite{get;set;}
    公共字符串IsPrimaryKey{get;set;}
    公共字符串IsDeltaField{get;set;}
    公共字符串为索引{get;set;}
    公共字符串是瞬态的{get;set;}
    公共字符串是唯一的{get;set;}
    重写{get;set;}格式的公共字符串
    公共字符串可锁定{get;set;}
    公共字符串方向{get;set;}
    公共字符串值SetByDatabase{get;set;}
    公共字符串FormatScale{get;set;}
    公共字符串格式掩码{get;set;}
    公共字符串负格式{get;set;}
    公共字符串组{get;set;}
    公共字符串聚合函数{get;set;}
    公共字符串从副本{get;set;}中排除
    公共字符串IsExpression{get;set;}
    公共字符串FriendlyName{get;set;}
    公共字符串可浏览{get;set;}
    公共字符串可查询{get;set;}
    公共字符串IsEnumeration{get;set;}
    公共字符串IsAddInPrimaryKey{get;set;}
    公共字符串AddInTableName{get;set;}
    公共字符串AddInRelationField{get;set;}
    公共字符串IsMember{get;set;}
    公共字符串从重置{get;set;}中排除
    }
    }
    

    使用以下类别:

        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; }
    
        }
    
    公共类PersistentObject
    {
    [XmlAttribute(“名称”)]
    公共字符串名称{get;set;}
    公共字符串表名{get;se
    
     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; }
        }
    }
    
        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; }
    
        }