简体   繁体   English

我应该使用哪种类的模型来生成此Xml结构? (C#)

[英]What Model of classes should I use to generate this Xml structure? (C#)

I need to create a model that will serialize to an Xml of this form (this is part of a bigger Xml): 我需要创建一个模型,该模型将序列化为这种形式的Xml(这是更大Xml的一部分):

<ManufacturerPartNumbers>
    <ManufacturerPartNumber>26-12345-8W</ManufacturerPartNumber>
    <ManufacturerPartNumber>26-12345-8Y</ManufacturerPartNumber>
    <ManufacturerPartNumber>26-12345-8Z</ManufacturerPartNumber>
</ManufacturerPartNumbers>

I've tried using 我试过使用

[XmlType(TypeName = "ManufacturerPartNumber")]
public class ManufacturerPartNumberModel
{
    public string Number { get; set; }
}

And in the upper class: 在上层阶级中:

public List<ManufacturerPartNumberModel> ManufacturerPartNumbers { get; set; }

But this generates an extra Xml Node: 但这会生成一个额外的Xml节点:

<ManufacturerPartNumbers>
    <ManufacturerPartNumber>
      <Number>26-12345-8W</Number>
    </ManufacturerPartNumber>
</ManufacturerPartNumbers>

And 5 minutes later I found the answer :) 5分钟后,我找到了答案:)

I just had to add [XmlTextAttribute] to the Number property and it will serialize it as a nameless inline attribute inside ManufacturerPartNumberModel 我只需要将[XmlTextAttribute]添加到Number属性中,它将把它序列化为ManufacturerPartNumberModel内部的无名内联属性。

[XmlType(TypeName = "ManufacturerPartNumber")]
    public class ManufacturerPartNumberModel
    {
        [XmlTextAttribute]
        public string Number { get; set; }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM