Link to home
Create AccountLog in
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

ASKER CERTIFIED SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of sbornstein2
sbornstein2

ASKER

It seemed to do the same thing no change
still shows:

{
        "workflowName": {
          "workflowName": "string"
        }
      }
I changed this just to test it like this:

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

and its showing:

     {
        "workflowName": {
          "workflowNameF": "string"
        }
      }
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 ?
Also seems the create-workflow root is not serializing and showing in the XML
That is the json in swagger yes I want to serlialize to what you are showing as the XML.
Also trying to see when I need to use XmlInclude at all and how that works
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 ?
The classes are setup but when using swagger the example is in Json format and that is where I see the duplicate workflowname
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 ?