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):

<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:

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

Source: New Questions

1 Comment

  1. Digin Dominic

    I just had to add [XmlTextAttribute] to the Number property and it will serialize it as a nameless inline attribute inside ManufacturerPartNumberModel
    [XmlType(TypeName = “ManufacturerPartNumber”)]
    public class ManufacturerPartNumberModel
    {
    [XmlTextAttribute]
    public string Number { get; set; }
    }

    Reply

Leave a Reply