1

I am trying to convert XML string to C# object, I have json string acle in xml tag, as shown below,

<message> <data:gcm xmlns:data=\"google:mobile:data\">{\"message_type\":\"ack\",\"from\":\"sdhad4asd4a-sdasd45ds\",\"message_id\":\"-something\"}</data:gcm> </message>

I want json string from data tag I just want this string from above xml,

{\"message_type\":\"ack\",\"from\":\"sdhad4asd4a-sdasd45ds\",\"message_id\":\"-something\"}

So how can I get this using c#.?

Thank you in advance.

2
  • If the value of the element in the XML is already JSON, then it sounds like you're really just asking "How do I get the value of a specific element in XML" right? Nothing JSON-specific. I suggest you read a LINQ to XML tutorial. Commented Oct 13, 2016 at 10:50
  • @JonSkeet yes you are right I want value of XML element, Let me check with suggestion, and thank you for quick response. appreciated.! Commented Oct 13, 2016 at 10:54

1 Answer 1

2

By reading some LINQ to XML documents I got the solution which is like below,

XDocument xdoc = new XDocument();
xdoc = XDocument.Parse(msg.ToString());

var result = xdoc.Element("message").Descendants();

var myString = result.FirstOrDefault().Value; //This will out given json string

Again Thank you @JonSkeet for your suggestion.!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.