如何在 C# 模型中命名我的 XML header 'header list-id: "inventory"'?

How do I name my XML header 'header list-id: "inventory"' in a C# model?

我正在处理的项目的第一步是根据记录列表创建一个 XML 文件。以前是用java写的,现在改成C#了。他们在 java 中的做法是显式地写出每一行,我正在尝试合并一种以编程方式执行此操作的方法,以便可以在以后的部署步骤中更改这些值。

我必须复制的文件的 header 名称为 header list-id: "inventory".

我有它只是写 'header' 很好,但我不知道如何包括其余部分。

    [Serializable()]
    [DesignerCategory("code")]
    [XmlType(AnonymousType = false, Namespace = "http://www.demandware.com/xml/impex/inventory/2007-05-31")]
    [XmlRoot(Namespace = "http://www.demandware.com/xml/impex/inventory/2007-05-31", IsNullable = false)]
    public partial class inventory
    {

        private inventoryInventorylist inventorylistField;

        [XmlElement("inventory-list")]
        public inventoryInventorylist inventorylist
        {
            get
            {
                return inventorylistField;
            }
            set
            {
                inventorylistField = value;
            }
        }
    }

    [Serializable()]
    [DesignerCategory("code")]
    [XmlType(AnonymousType = true, TypeName = "inventory-list")]
    public partial class inventoryInventorylist
    {
        public inventoryInventorylistHeader header;

        private inventoryInventorylistHeader headerField
        {
            get
            {
                return header;
            }
            set
            {
                header = value;
            }
        }
    }

    [Serializable()]
    [DesignerCategory("code")]
    [XmlType(AnonymousType = false, TypeName = "header")]
    public partial class inventoryInventorylistHeader 
    {

        [XmlElement("default-instock")]
        public bool defaultinstock
        {
            get
            {
                return defaultinstockField;
            }
            set
            {
                defaultinstockField = false;
            }
        }

        [XmlElement("description")]
        public string description
        {
            get
            {
                return descriptionField;
            }
            set
            {
                descriptionField = "Inventory";
            }
        }

        [XmlElement("use-bundle-inventory-only")]
        public bool usebundleinventoryonly
        {
            get
            {
                return usebundleinventoryonlyField;
            }
            set
            {
                usebundleinventoryonlyField = false;
            }
        }

        [XmlElement("on-order")]
        public bool onorder
        {
            get
            {
                return onorderField;
            }
            set
            {
                onorderField = false;
            }
        }
}

创作:

 var inventory = new inventory()
 {
     inventorylist = new inventoryInventorylist()
     {
          header = new inventoryInventorylistHeader()
          {
              defaultinstock = false,
              description = "Inventory",
              usebundleinventoryonly = false,
              onorder = false
          }
     }
 }

Returns这个:

<inventory xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.demandware.com/xml/impex/inventory/2007-05-31">
  <inventory-list>
    <header>
      <default-instock>false</default-instock>
      <description>Inventory</description>
      <use-bundle-inventory-only>false</use-bundle-inventory-only>
      <on-order>false</on-order>
    </header>

我想要它 return 这个:

<inventory xmlns="http://www.demandware.com/xml/impex/inventory/2007-05-31">
  <inventory-list>
    <header list-id="inventory">
      <default-instock>false</default-instock>
      <description>Inventory</description>
      <use-bundle-inventory-only>false</use-bundle-inventory-only>
      <on-order>false</on-order>
    </header>

如果我在模型中这样写:

public partial class inventoryInventorylist
    {
        [XmlElement("header list-id:\"inventory\"")]
        public inventoryInventorylistHeader header;

然后变成这样:

<inventory-list>
    <header_x0020_list-id_x003A__x0022_inventory_x0022_>
      <default-instock>false</default-instock>
      <description>Inventory</description>
      <use-bundle-inventory-only>false</use-bundle-inventory-only>
      <on-order>false</on-order>
    </header_x0020_list-id_x003A__x0022_inventory_x0022_>

我如何在序列化模型时强制它显示所需的文本?

您可以为此使用 XmlAttribute 属性。 如果您添加

[XmlAttribute("list-id")]
public string listid
{
    get;
    set;
}

到您的 inventoryInventorylistHeader class,您将获得问题中所述的 xml。

我通常做的是使用 xml 并使用 visual studio 生成 xsd。 然后我将打开一个 vs 开发人员命令提示符并使用 xsd.exe 生成 classes。然后可以稍微重构这些代码以在代码中看起来更好。