0

In .NET Coew 3.1 I am trying to generate an XML file of the following class, but without the orders tag showing in the XML file, meaning I have the root tag, than only the list of items

[XmlRoot("data")]
public class PurchaseOrder
{
    [XmlElement("orders")]
    public Item [] ItemsOrders;
}
[XmlRoot("item")]
public class Item
{
    [XmlElement("itemid")]
    public string ItemID;
    [XmlElement("price")]
    public decimal ItemPrice;
}

would result in

<data xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <orders>
        <item>
            <itemid>aaa111</itemid>
            <ItemPrice>34.22</ItemPrice>
        </item>
        <item>
            <itemid>bbb222</itemid>
            <price>2.89</price>
        </item>
    </orders>
</data>

but I need to remove the orders tag. The result I need to produce is

<data xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <item>
        <itemid>aaa111</itemid>
        <ItemPrice>34.22</ItemPrice>
    </item>
    <item>
        <itemid>bbb222</itemid>
        <price>2.89</price>
    </item>
</data>
Sugafree
  • 631
  • 2
  • 14
  • 30
  • What you posted as actual result is wrong. You have [XmlElement("orders")] public Item [] ItemsOrders; You should have [XmlElement("item")] public Item [] ItemsOrders; The current code would not have both "orders" and "items". Instead the current code would not have orders, and the item tags would be orders. Using XmlElement with an array only produces one xml tag while your code show two tags. – jdweng May 31 '20 at 17:07

1 Answers1

1

The best way to get the correct classes you require would be to copy the XML you want to the clipboard, and then (in VS2019) Edit/Paste Special/Paste Classes As XML.

This will create the correct classes for the XML you supplied.

Neil
  • 11,059
  • 3
  • 31
  • 56