I have a JSON like below:
{
"method": "abc",
"version": "1.0.1",
"login": "123456",
"password": "abc123",
"referenceNumber": "1258",
"amount": 1.00,
"routingNumber": "145879",
"accountNumber": "145879",
"name": "abc as",
"authorizationID": "14789",
"savings": true,
"address1": "noida",
"city": "delhi",
"state": "ab",
"zip": "578945",
"phone": "1234567890",
"email": "abc@test.com",
"test": true}
I want it to convert it into XML, for that I wrote the following code, here MLPayment is the JSON:
XmlSerializer xsSubmit = new XmlSerializer(typeof(MLPayment));
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", "");
String Body = null;
//XmlDocument dox = new XmlDocument();
using (StringWriter stringWriter = new StringWriter())
{
xsSubmit.Serialize(stringWriter, mLPayment, ns);
Body = stringWriter.ToString();
}
\\n\\r
, response is as below"<?xml version=\\"1.0\\" encoding=\\"utf-16\\"?>\\r\\n<ACH>\\r\\n <Method>Debit</Method>\\r\\n <Version>1</Version>\\r\\n <Login>9</Login>\\r\\n <Password>uadwaadwadwu</Password>\\r\\n <ReferenceNumber>123456</ReferenceNumber>\\r\\n <Amount>1.00</Amount>\\r\\n <RoutingNumber>12</RoutingNumber>\\r\\n <AccountNumber>123456</AccountNumber>\\r\\n <Name>Joe Blow </Name>\\r\\n <AuthorizationID>123456</AuthorizationID>\\r\\n <Savings>true</Savings>\\r\\n <Address1>SUITE 230adw</Address1>\\r\\n <City>abc</City>\\r\\n <State>AZ</State>\\r\\n <Zip>8504445</Zip>\\r\\n <Phone>1234567890</Phone>\\r\\n <Email>test@abc.com</Email>\\r\\n <Test>true</Test>\\r\\n</ACH>"
to fix second issue, I replaced the \\n\\r
String cleanedReturnedValue = Body.Replace("\r", "").Replace("\n", "");
String cR = System.Text.RegularExpressions.Regex.Unescape(cleanedReturnedValue);
after that output is:
"<?xml version=\\"1.0\\" encoding=\\"utf-16\\"?><ACH> <Method>Debit</Method> <Version>1</Version> <Login>9</Login> <Password>uadwaadwadwu</Password> <ReferenceNumber>123456</ReferenceNumber> <Amount>1.00</Amount> <RoutingNumber>12</RoutingNumber> <AccountNumber>123456</AccountNumber> <Name>Joe Blow </Name> <AuthorizationID>123456</AuthorizationID> <Savings>true</Savings> <Address1>SUITE 230adw</Address1> <City>abc</City> <State>AZ</State> <Zip>8504445</Zip> <Phone>1234567890</Phone> <Email>test@abc.com</Email> <Test>true</Test></ACH>"
but the 1st problem still remains,
<?xml version=\\"1.0\\" encoding=\\"utf-16\\"?>
You are almost there.
Just use LINQ to XML, and load your string into XDocument.
c#
void Main()
{
XDocument xdoc = XDocument.Parse("<?xml version=\"1.0\" encoding=\"utf-16\"?><ACH> <Method>Debit</Method> <Version>1</Version> <Login>9</Login> <Password>uadwaadwadwu</Password> <ReferenceNumber>123456</ReferenceNumber> <Amount>1.00</Amount> <RoutingNumber>12</RoutingNumber> <AccountNumber>123456</AccountNumber> <Name>Joe Blow </Name> <AuthorizationID>123456</AuthorizationID> <Savings>true</Savings> <Address1>SUITE 230adw</Address1> <City>abc</City> <State>AZ</State> <Zip>8504445</Zip> <Phone>1234567890</Phone> <Email>test@abc.com</Email> <Test>true</Test></ACH>");
}
You can use Newtonsoft.json package to serialize json to xml or vice versa. The following method will convert Json to XML.
XNode node = JsonConvert.DeserializeXNode(json, "Root");
https://www.c-sharpcorner.com/blogs/how-to-convert-json-into-xml-or-xml-into-json
https://www.newtonsoft.com/json/help/html/ConvertJsonToXml.htm
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.