c# - XML Schema 发送带有结束标签和默认值根名称的空标签
问题描述
我有以下我希望复制的 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);
}
问题。
- 如何添加根节点 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; }
}
}
解决方案
使用以下类:
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; }
}
推荐阅读
- python - Azure PageBlob 上传新文件:x-ms-blob-content-length 错误
- angular - 使用角度将产品ID从客户端添加到服务器
- laravel - 重定向到 livewire 组件时的问题
- c++ - 无法使用工具集 v120 (Visual Studio) 转换 std::pair
- javascript - 在表单 php 上显示 JS 警报
- reactjs - React + Mutual TLS (TLS) 与客户端证书到 REST 微服务
- javascript - 如何让所有图标单独旋转而不是一个?
- navigation - 引导药丸如何显示药丸标签而不在引导程序5上滚动
- node.js - Nodejs NetSuite-rest模块:请求正文中的内容无效
- discord.py - 删除 Discord Markdown