<employee>
<id>34</id>
<name>Sam</name>
<Age>45</Age>
</employee>
i want to convert to Json string like this
{
"id":34,
"name":"Sam",
"Age":45;
}
And where is the code that you tried? I ask because this question has been answered many times in your previous threads. I have a feeling your purposefully trying to be disruptive by asking the same question over and over.
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Sockets;
using System.Reflection;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
namespace ConsoleCs
{
class Program
{
static void Main(string[] args)
{
string xmlDocumentText = "<employee><id>34</id><name>Sam</name><Age>45</Age></employee>";
XmlSerializer serializer = new XmlSerializer(typeof(employee));
employee emp;
using (StringReader reader = new StringReader(xmlDocumentText))
{
emp = (employee)(serializer.Deserialize(reader));
}
string json = JsonConvert.SerializeObject(emp);
Console.WriteLine(json);
}
}
// NOTE: Generated code may require at least .NET Framework 4.5 or .NET Core/Standard 2.0.
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class employee
{
private byte idField;
private string nameField;
private byte ageField;
/// <remarks/>
public byte id
{
get
{
return this.idField;
}
set
{
this.idField = value;
}
}
/// <remarks/>
public string name
{
get
{
return this.nameField;
}
set
{
this.nameField = value;
}
}
/// <remarks/>
public byte Age
{
get
{
return this.ageField;
}
set
{
this.ageField = value;
}
}
}
}
You could also use JsonConvert.SerializeXmlNode and JObject to meet your requirement.
Below is my sample.
XmlDocument doc = new XmlDocument();
doc.LoadXml("<employee><id>34</id><name>Sam</name><Age>45</Age></employee>"); // load the xml
string json= JsonConvert.SerializeXmlNode(doc); // use SerializeXmlNode to convert the xml to json string
json = Regex.Replace(json, "\"(\\d+)\"","$1"); // replace "34","45" with 35 ,45 using regex, or Jobject will parse it as string
string result = JObject.Parse(json)["employee"].ToString(); // get the employee property of JObject Response.Write(result);;
The result.
Best regards,
Ackerly Xu
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
You could get the root tag name through XmlDocument.
XmlDocument doc = new XmlDocument();
doc.LoadXml("<employee><id>34</id><name>Sam</name><Age>45</Age></employee>"); // load the xml
string json = JsonConvert.SerializeXmlNode(doc); // use SerializeXmlNode to convert the xml to json string
json = Regex.Replace(json, "\"(\\d+)\"", "$1"); // replace "34","45" with 35 ,45 using regex, or Jobject will parse it as string
string result = JObject.Parse(json)[doc.FirstChild.Name].ToString(); // get the employee property of JObject
Response.Write(result); ;
Best regards,
Ackerly Xu
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
Member
6 Points
246 Posts
Given XML string convert to Json string using newtonsoft
Feb 12, 2019 03:15 PM|Guhananth|LINK
I have XML .want to convert to Json string that looks
(
"Id":12,
)
All-Star
53641 Points
24007 Posts
Re: Given XML string convert to Json string using newtonsoft
Feb 12, 2019 03:30 PM|mgebhard|LINK
The JSON is invalid. This is the proper format.
Post your source code, explain the expected results, and explain the actual results.
Member
6 Points
246 Posts
Re: Given XML string convert to Json string using newtonsoft
Feb 12, 2019 05:26 PM|Guhananth|LINK
Hi,
Given xml like this
All-Star
53641 Points
24007 Posts
Re: Given XML string convert to Json string using newtonsoft
Feb 12, 2019 08:23 PM|mgebhard|LINK
And where is the code that you tried? I ask because this question has been answered many times in your previous threads. I have a feeling your purposefully trying to be disruptive by asking the same question over and over.
Member
6 Points
246 Posts
Re: Given XML string convert to Json string using newtonsoft
Feb 13, 2019 02:16 AM|Guhananth|LINK
Given xml is dynamically coming from external source. we do not know it has Employee tag. it can be any other tag. how to handle.
Contributor
3500 Points
1300 Posts
Re: Given XML string convert to Json string using newtonsoft
Feb 13, 2019 02:57 AM|Ackerly Xu|LINK
Hi Guhananth,
You could also use JsonConvert.SerializeXmlNode and JObject to meet your requirement.
Below is my sample.
The result.
Best regards,
Ackerly Xu
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
Member
6 Points
246 Posts
Re: Given XML string convert to Json string using newtonsoft
Feb 13, 2019 05:05 AM|Guhananth|LINK
Contributor
3500 Points
1300 Posts
Re: Given XML string convert to Json string using newtonsoft
Feb 13, 2019 05:48 AM|Ackerly Xu|LINK
Hi Guhananth,
You could get the root tag name through XmlDocument.
XmlDocument doc = new XmlDocument(); doc.LoadXml("<employee><id>34</id><name>Sam</name><Age>45</Age></employee>"); // load the xml string json = JsonConvert.SerializeXmlNode(doc); // use SerializeXmlNode to convert the xml to json string json = Regex.Replace(json, "\"(\\d+)\"", "$1"); // replace "34","45" with 35 ,45 using regex, or Jobject will parse it as string string result = JObject.Parse(json)[doc.FirstChild.Name].ToString(); // get the employee property of JObject Response.Write(result); ;
Best regards,
Ackerly Xu
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.