Convert xml to json string and class c#



Once you have the raw XML, you can create an XmlDocument object (XmlDocument lives in the System.Xml namespace).
and JSON DeserializeObject - using Newtonsoft.Json;

 
string xml = @"<PidData><Resp errCode="0" errInfo="" fCount="1" fType="0" iCount="" pCount="" pgCount="" pTimeout="" nmPoints="45" qScore="47
"/><DeviceInfo dpId="Morpho.SmartChip" rdsId="SCPL.WIN.001" rdsVer="1.0.5" dc="89b981c1-83c4-4b04-a42a-2fdf6aea73e4" mi="MSO1300E2L0SW" mc="d232"><additional_info><Param name="srno" value="1701I236417"/></additional_info></DeviceInfo>
<Skey ci="20221021">1222</Skey><Hmac>12</Hmac><Data type="X">3ddd</Data></PidData>";
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
string json = JsonConvert.SerializeXmlNode(doc);
DeviceDetailRoot deviceDetailRoot = JsonConvert.DeserializeObject<DeviceDetailRoot>(json.ToString());
 
 
This is a class of XML objects, you can create your self or just use XML to class online convert.
public partial class PidData
{
[JsonProperty("Resp")]
public Resp Resp { get; set; }
[JsonProperty("DeviceInfo")]
public DeviceInfo DeviceInfo { get; set; }
[JsonProperty("Skey")]
public Skey Skey { get; set; }
[JsonProperty("Hmac")]
public string Hmac { get; set; }
[JsonProperty("Data")]
public Datas Data { get; set; }
}
public partial class Datas
{
[JsonProperty("@type")]
public string Type { get; set; }
[JsonProperty("#text")]
public string Text { get; set; }
}
public partial class DeviceInfo
{
[JsonProperty("@dpId")]
public string DpId { get; set; }
[JsonProperty("@rdsId")]
public string RdsId { get; set; }
[JsonProperty("@rdsVer")]
public string RdsVer { get; set; }
[JsonProperty("@dc")]
public string Dc { get; set; }
[JsonProperty("@mi")]
public string Mi { get; set; }
[JsonProperty("@mc")]
public string Mc { get; set; }
[JsonProperty("additional_info")]
public AdditionalInfo AdditionalInfo { get; set; }
}
public partial class AdditionalInfo
{
[JsonProperty("Param")]
public Param Param { get; set; }
}
public partial class Param
{
[JsonProperty("@name")]
public string Name { get; set; }
[JsonProperty("@value")]
public string Value { get; set; }
}
public partial class Resp
{
[JsonProperty("@errCode")]
public string ErrCode { get; set; }
[JsonProperty("@errInfo")]
public string ErrInfo { get; set; }
[JsonProperty("@fCount")]
public string FCount { get; set; }
[JsonProperty("@fType")]
public string FType { get; set; }
[JsonProperty("@iCount")]
public string ICount { get; set; }
[JsonProperty("@pCount")]
public string PCount { get; set; }
[JsonProperty("@pgCount")]
public string PgCount { get; set; }
[JsonProperty("@pTimeout")]
public string PTimeout { get; set; }
[JsonProperty("@nmPoints")]
public string NmPoints { get; set; }
[JsonProperty("@qScore")]
public string QScore { get; set; }
}
public partial class Skey
{
[JsonProperty("@ci")]
public string Ci { get; set; }
[JsonProperty("#text")]
public string Text { get; set; }
}
 
 
 
 
 
 

0 Comment's

Comment Form

Submit Comment