I have the following XML and I need to serialize it into a POCO and then deserialize it to JSON. Here's the XML:
<PAC xmlns="https://mynamespace.com">
<Creds>
<AID>1019766</AID>
<AToken>290D56096864849B</AToken>
<AcID>01234</AcID>
</Creds>
<App>
<AppID>133</AppID>
<AppName>XML</AppName>
<AppVer>1.1.1</AppVer>
</App>
<PAcct>
<PAcctType>0</PAcctType>
<PAcctRefNum>000001</PAcctRefNum>
</PAcct>
<CD>
<CN>4445_220007</CN>
<EM>12</EM>
<EY>25</EY>
<C3></C3>
</CD>
</PAC>
Here's the object(s) that I'm attempting to populate from the XML and then deserialize to JSON. CCS and PAC are base objects derived from TObjects, which contains all the other objects.
[XmlRoot("CCS", Namespace = "https://mynamespace.com")]
public partial class CCS : TObjects
{
public TTypeID TTypeID { get; } = TTypeID.CCS; // Enum for TTypes
}
[XmlRoot("PAC", Namespace = "https://mynamespace.com")]
public partial class PAC : TObjects
{
public TTypeID TTypeID { get; } = TTypeID.PAC; // Enum for TTypes
}
public partial class TObjects
{
public Creds? Creds { get; set; }
public App? App { get; set; }
public PAcct? PAcct { get; set; }
public CD? CD { get; set; }
}
public partial class App
{
public int? AppID { get; set; }
public string? AppName { get; set; }
public string? AppVer { get; set; }
}
public partial class Creds
{
public int? AID { get; set; }
public string? AToken { get; set; }
public string? AcID { get; set; }
}
public partial class PAcct
{
public int? PAcctID { get; set; }
public string? PAcctType { get; set; }
public string? PAcctRefNum { get; set; }
}
public partial class CD
{
public string? T1 { get; set; }
public string? T2 { get; set; }
public string? CN { get; set; }
public CL? CL { get; set; } // CL is Enum
public int? EM { get; set; }
public int? EY { get; set; }
public int? C3 { get; set; }
}
I've been using Newtonsoft's SerilizeXmlNode() with this code; this code removes the namespace before doing the serialization.
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(System.Text.RegularExpressions.Regex.Replace(xmlString.Trim(), @"(xmlns:?[^=]*=[""][^""]*[""])", "",
System.Text.RegularExpressions.RegexOptions.IgnoreCase | System.Text.RegularExpressions.RegexOptions.Multiline));
string jsonText = JsonConvert.SerializeXmlNode(xmlDoc.FirstChild, Newtonsoft.Json.Formatting.Indented);
It produces a valid Json string but the integers are all strings and it does not recognize empty values in the XML.
I need to also be able to pass in other XML and serialize to their corresponding objects derived from TObjects, like CCS above, and then deserialize to Json.
How can I convert the above XML into this Json string and ignore the empty values and formatted with the classname as the root?
{
"PAC": {
"Creds": {
"AID": 1019766,
"AToken": "290D56096864849B",
"AcID": "01234"
},
"App": {
"AppID": "133",
"AppName": "XML",
"AppVer": "1.1.1"
},
"PAcct": {
"PAcctType": "0",
"PAcctRefNum": "000001"
},
"Card": {
"CN": "4445_220007",
"EM": 12,
"EY": 25,
}
}
}
is there a reason to use class as interstitial step?
option without interstitial class
result:
update full example to convert. dont need namespaces
and result would be