Avatar of sbornstein2
sbornstein2
 asked on

XML Simple Question for C# Devs

I need to get the following format for serializing XML but not sure how best to handle this.  Element tag needed:

<create-by-workflow>
    <workflow-name>Customer</workflow-name>
</create-by-workflow>

Open in new window


Right now I have two classes as such:

public class WorkflowRequestDto
{
    [XmlElement(Constants.CreateByWorkflowTagName)] //this is     public const string CreateByWorkflowTagName = "create-by-workflow";
    public WorkflowNameDto WorkflowName { get; set; }
}

public class WorkflowNameDto
{
    [XmlElement(Constants.WorkflowNameTagName)] // this is     public const string WorkflowNameTagName = "workflow-name";
    public string WorkflowName { get; set; }
}

Open in new window


The JSON looks like this in Swagger:

"workflowName": {
    "workflowName": "string"
}

Open in new window


Is there a way to have it just be  "workflowName": "string" to the user so something along these lines although this does not work:

[XmlElement(Constants.CreateByWorkflowTagName)] 
[XmlElement(Constants.WorkflowNameTagName)]
public string WorkflowName { get; set; }

Open in new window

C#XMLJSON

Avatar of undefined
Last Comment
Chris Stanyon

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Chris Stanyon

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
sbornstein2

ASKER
It seemed to do the same thing no change
sbornstein2

ASKER
still shows:

{
        "workflowName": {
          "workflowName": "string"
        }
      }
sbornstein2

ASKER
I changed this just to test it like this:

    [XmlText]
    public string WorkflowNameF { get; set; }

and its showing:

     {
        "workflowName": {
          "workflowNameF": "string"
        }
      }
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Chris Stanyon

That's JSON, not XML - I thought your question was about serialzing XML  to the following format:

<create-by-workflow>
    <workflow-name>Customer</workflow-name>
</create-by-workflow>

The code I posted does that.

Where does JSON come into it ?
sbornstein2

ASKER
Also seems the create-workflow root is not serializing and showing in the XML
sbornstein2

ASKER
That is the json in swagger yes I want to serlialize to what you are showing as the XML.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
sbornstein2

ASKER
Also trying to see when I need to use XmlInclude at all and how that works
Chris Stanyon

I'm slightly confused. You have 2 classes and your question was asking how to serialize those 2 classes to XML. I've shown you how to do that.

Am I missing something ?
sbornstein2

ASKER
The classes are setup but when using swagger the example is in Json format and that is where I see the duplicate workflowname
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Chris Stanyon

OK. I must be missing some vital piece of information here. You asked how to serialize a C# class into an XML, which I've shown you how to do, but you keep referring to JSON without explaining how that fits in with your question.

Currently you have 2 classes, and I've shown you how to serialize those to the XML that you requested using the attributes. Can you explain how Swagger and JSON fits into that. Are you wanting to serialize the classes into JSON ? Are you wanting to serialize the XML into JSON ?