我应该使用 类 的什么模型来生成这个 Xml 结构? (C#)
What Model of classes should I use to generate this Xml structure? (C#)
我需要创建一个模型,它将序列化为这种形式的 Xml(这是更大的 Xml 的一部分):
<ManufacturerPartNumbers>
<ManufacturerPartNumber>26-12345-8W</ManufacturerPartNumber>
<ManufacturerPartNumber>26-12345-8Y</ManufacturerPartNumber>
<ManufacturerPartNumber>26-12345-8Z</ManufacturerPartNumber>
</ManufacturerPartNumbers>
我试过使用
[XmlType(TypeName = "ManufacturerPartNumber")]
public class ManufacturerPartNumberModel
{
public string Number { get; set; }
}
而上层class:
public List<ManufacturerPartNumberModel> ManufacturerPartNumbers { get; set; }
但这会生成一个额外的 Xml 节点:
<ManufacturerPartNumbers>
<ManufacturerPartNumber>
<Number>26-12345-8W</Number>
</ManufacturerPartNumber>
</ManufacturerPartNumbers>
5 分钟后我找到了答案:)
我只需要将 [XmlTextAttribute]
添加到数字 属性 中,它会将其序列化为 ManufacturerPartNumberModel
中的无名内联属性
[XmlType(TypeName = "ManufacturerPartNumber")]
public class ManufacturerPartNumberModel
{
[XmlTextAttribute]
public string Number { get; set; }
}
我需要创建一个模型,它将序列化为这种形式的 Xml(这是更大的 Xml 的一部分):
<ManufacturerPartNumbers>
<ManufacturerPartNumber>26-12345-8W</ManufacturerPartNumber>
<ManufacturerPartNumber>26-12345-8Y</ManufacturerPartNumber>
<ManufacturerPartNumber>26-12345-8Z</ManufacturerPartNumber>
</ManufacturerPartNumbers>
我试过使用
[XmlType(TypeName = "ManufacturerPartNumber")]
public class ManufacturerPartNumberModel
{
public string Number { get; set; }
}
而上层class:
public List<ManufacturerPartNumberModel> ManufacturerPartNumbers { get; set; }
但这会生成一个额外的 Xml 节点:
<ManufacturerPartNumbers>
<ManufacturerPartNumber>
<Number>26-12345-8W</Number>
</ManufacturerPartNumber>
</ManufacturerPartNumbers>
5 分钟后我找到了答案:)
我只需要将 [XmlTextAttribute]
添加到数字 属性 中,它会将其序列化为 ManufacturerPartNumberModel
[XmlType(TypeName = "ManufacturerPartNumber")]
public class ManufacturerPartNumberModel
{
[XmlTextAttribute]
public string Number { get; set; }
}