Cinchoo/ChoETL

ChoXmlWriter - Array item names

Opened this issue · 0 comments

Hello,

Is there a way to customize the array item names produced when converting from JSON to XML?

For example:

var json = @"
[
    {
        ""name"": ""John"",
        ""age"": 30,
        ""address"": {
            ""city"": ""New York"",
            ""street"": ""5th Avenue"",
            ""number"": 123
        },
        ""phones"": [
            {
                ""type"": ""Home"",
                ""number"": ""123-456-7890""
            },
            {
                ""type"": ""Work"",
                ""number"": ""456-789-0123""
            }
        ]
    }
]";

var sb = new StringBuilder();
using (var xml = new ChoXmlWriter(sb).Configure(x =>
{
    x.RootName = "root";
    x.NodeName = "item";
    x.UseXmlArray = true;
    x.DoNotEmitXmlNamespace = true;
    x.ThrowAndStopOnMissingField = false;
}))
using (var r = new ChoJSONReader(JToken.Parse(json)))
{
    xml.Write(r);
}

Console.WriteLine(sb.ToString());

Produces:

<root>
  <item>
    <name>John</name>
    <age>30</age>
    <address>
      <city>New York</city>
      <street>5th Avenue</street>
      <number>123</number>
    </address>
    <phones>
      <phone>
        <type>Home</type>
        <number>123-456-7890</number>
      </phone>
      <phone>
        <type>Work</type>
        <number>456-789-0123</number>
      </phone>
    </phones>
  </item>
</root>

But I need the individual phone nodes to have the tag name "item" like this:

<root>
  <item>
    <name>John</name>
    <age>30</age>
    <address>
      <city>New York</city>
      <street>5th Avenue</street>
      <number>123</number>
    </address>
    <phones>
      <item>
        <type>Home</type>
        <number>123-456-7890</number>
      </item>
      <item>
        <type>Work</type>
        <number>456-789-0123</number>
      </item>
    </phones>
  </item>
</root>

This is just a simple example but I am trying to generically handle any json structure so I can't hardcode any rules or assume the shape of the data.

Thanks!