Hi @Kmcnet , Welcome to Microsoft Q&A,
Do you want this effect?
First, make sure to install the Newtonsoft.Json NuGet package if you haven't already.
The JsonProperty attribute is used to specify the JSON property names, and the structure follows the desired format. You can customize the class structure based on the specific requirements of your JSON structure.
using Newtonsoft.Json;
using System;
namespace xxx
{
public class To
{
[JsonProperty("@Qualifier")]
public string Qualifier { get; set; }
[JsonProperty("#text")]
public string Text { get; set; }
}
public class Header
{
public To To { get; set; }
}
public class Message
{
[JsonProperty("@xmlns:xsi")]
public string XmlnsXsi { get; set; }
[JsonProperty("@DatatypesVersion")]
public string DatatypesVersion { get; set; }
[JsonProperty("@TransportVersion")]
public string TransportVersion { get; set; }
[JsonProperty("@TransactionDomain")]
public string TransactionDomain { get; set; }
[JsonProperty("@TransactionVersion")]
public string TransactionVersion { get; set; }
[JsonProperty("@StructuresVersion")]
public string StructuresVersion { get; set; }
[JsonProperty("@ECLVersion")]
public string ECLVersion { get; set; }
public Header Header { get; set; }
}
internal class Program
{
static void Main(string[] args)
{
// Create an instance of the Message class with sample data
Message message = new Message
{
XmlnsXsi = "http://www.w3.org/2001/XMLSchema-instance",
DatatypesVersion = "20170715",
TransportVersion = "20170715",
TransactionDomain = "SCRIPT",
TransactionVersion = "20170715",
StructuresVersion = "20170715",
ECLVersion = "20170715",
Header = new Header
{
To = new To
{
Qualifier = "P",
Text = "7654321"
}
}
};
// Convert the Message instance to JSON
string json = JsonConvert.SerializeObject(message);
// Display the generated JSON
Console.WriteLine(json);
Console.ReadLine();
}
}
}
Best Regards,
Jiale
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.