Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ChoXmlWriter - Array item names #316

Open
k-schneider opened this issue Apr 15, 2024 · 0 comments
Open

ChoXmlWriter - Array item names #316

k-schneider opened this issue Apr 15, 2024 · 0 comments

Comments

@k-schneider
Copy link

k-schneider commented Apr 15, 2024

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant