Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
string msdg = "{"EventTimestamp":"2018-06-08 00:11:26,070", "Level":"INFO", "RouteId":"PubSubV2DistRoute",   
"ActivityTimestamp":"2018-06-07T23:13:50.363097Z", "SrcSys":"", "ShrActNr":"W0980X", "DtnCnyCd":"DE", "DtnFacMnm":"DESCK", "DtnSlicNr":"0445", "OrgCnyCd":"JP", "OrgFacMnm":"JPSHN", "OrgSlicNr":"1063", "IntSvcTypCd":"004", "ExtSvcTypCd":"012", "Message":"<?xml version="1.0" encoding="UTF-8" standalone="yes"?>"}


What I have tried:

I want to extract that xml message from the following string?

Thanks in advance.
Posted
Updated 12-Jun-18 10:41am
Comments
Maciej Los 12-Jun-18 16:38pm    
xml message? I see nothing related with xml data instead of "xml header" (declaration)...
Richard Deeming 14-Jun-18 14:32pm    
Lots of escape characters needed for that string! Plus a missing closing quote and semi-colon.

Either:
string msdg = "{\"EventTimestamp\":\"2018-06-08 00:11:26,070\", \"Level\":\"INFO\", \"RouteId\":\"PubSubV2DistRoute\",   
\"ActivityTimestamp\":\"2018-06-07T23:13:50.363097Z\", \"SrcSys\":\"\", \"ShrActNr\":\"W0980X\", \"DtnCnyCd\":\"DE\", \"DtnFacMnm\":\"DESCK\", \"DtnSlicNr\":\"0445\", \"OrgCnyCd\":\"JP\", \"OrgFacMnm\":\"JPSHN\", \"OrgSlicNr\":\"1063\", \"IntSvcTypCd\":\"004\", \"ExtSvcTypCd\":\"012\", \"Message\":\"<?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\" standalone=\\\"yes\\\"?>\"}";

Or:
string msdg = @"{""EventTimestamp"":""2018-06-08 00:11:26,070"", ""Level"":""INFO"", ""RouteId"":""PubSubV2DistRoute"",   
""ActivityTimestamp"":""2018-06-07T23:13:50.363097Z"", ""SrcSys"":"""", ""ShrActNr"":""W0980X"", ""DtnCnyCd"":""DE"", ""DtnFacMnm"":""DESCK"", ""DtnSlicNr"":""0445"", ""OrgCnyCd"":""JP"", ""OrgFacMnm"":""JPSHN"", ""OrgSlicNr"":""1063"", ""IntSvcTypCd"":""004"", ""ExtSvcTypCd"":""012"", ""Message"":""<?xml version=\""1.0\"" encoding=\""UTF-8\"" standalone=\""yes\""?>""}";

1 solution

First of all, that code won't compile as it is, even if you manage to have it compiled — by adding a @ at the beginning of string — you would need to just call the property Message and get everything you need. What was too difficult in that?

C#
dynamic obj = JsonConvert.DeserializeObject(msdg);

if(obj != null && obj.Message != null) {
    var xml = obj.Message;
    // use xml
}

I have an article that covers how to process JSON data in C# using JSON.NET library, please take a look at that article, From zero to hero in JSON with C#[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900