Click here to Skip to main content
14,974,357 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
Hi My service return below given structure

  "Description" : "Notification",
  "ID" : "465e24fc-eeed-58fa-a9ce-6b5108002f2e",
  "Topic" : "sns:us-asia-1:701722846169:fx-ma",
  "Agenda" : "fx-market-rates",
  "Key" : "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n<Quote xmlns:ns2=\"http://\" retrieval=\"2016-05-12T02:03:38Z\" type=\"MARKET\" sequence=\"10/95\" topic=\"f\">\n    <id>b24bc9a3-e2ab-491e-9399-689bfceb7707</id>\n    <rate>1.366000</rate>\n    <buy>USD</buy>\n    <sell>BND</sell>\n    <unit>USD</unit>\n</Quote>\n",
  "Timestamp" : "2016-05-12T02:04:29.232Z",
  "SignatureVersion" : "1",
  "Signature" : "FeKQf0JT3dr4N+",
  "SigningCertURL" : "",
  "UnsubscribeURL" : "https://snsc1"

First i want to read all attribute using C#. Second, Now I want to parse xml which Key attribute hold using C#. Can some one help me write code in c# for above given structure.

What I have tried:

First i want to read all attribute using C#. Second, Now I want to parse xml which Key attribute hold using C#. Can some one help me write code in c# for above given structure.
I tried json parse then xml parse using xdocument, xreader so on..
Thanks in advance
Updated 16-May-16 13:01pm
ZurdoDev 16-May-16 15:19pm
Looks like Json. Parse it as Json. It isn't xml.
Maciej Los 16-May-16 15:38pm
A 'Key' property/attribute contains xml data ;)
ZurdoDev 16-May-16 15:55pm
Oops, should have looked closer.

I'd recommend to start here: How to: Serialize and Deserialize JSON Data[^]

When you'll be able to get a Key property/member as a string, you can use XDocument class[^] for further xml proccessing.

Sorry, the description of your issue is not much informative...
Sergey Alexandrovich Kryukov 16-May-16 19:03pm
Hi Maciej,
I voted 4 this time, because some more information may be needed. I added some more links in Solution 2.
Maciej Los 17-May-16 2:21am
Hi Sergey!
Thank you for your comment. A 4 is quite reasonable, because it's hard to put more details in case when the question does not contain much description about issue. BTW: i do not see your 4.
Sure, I understand. The width of the topic is defined by lack of information on the subject. Additional information may change the choice you suggest, such as the choice of XDocument...
I'm not sure that you really need to deal with JSON (and XML) directly. It depends on the origin of data. If you also generate data in the file/stream, you certainly should better use Data Contract:
Using Data Contracts[^],
DataContractSerializer Class (System.Runtime.Serialization)[^],
DataContractJsonSerializer Class (System.Runtime.Serialization.Json)[^].

Even if you don't generate the data but have to use some data generated by 3rd party, it's often possible to design the data contract matching the data model in question and the peculiarities of XML of JSON representation given. It's more likely to succeed in case of JSON.

And, finally, let's consider the cases when using data contract technology is not feasible for the model represented in XML, JSON, or both. What to do? Well, then, unfortunately, it's likely that you would have to develop data model-specific mapping code (in contrast to data contract, which is fully model-agnostic).

As to JSON, please see my past answers:
haw to get data from Cloudant (json document)[^],
How To Convert object type to C# class object type[^],
how to conver multi level json data to C# Object?[^].

(Sorry, in these answers, I also write about JSON in JavaScript, but I recommend to understand that, too, even if it is unrelated to your problem; first, it's good to understand the JavaScript nature of JSON, secondly, when JSON is used, one of the reasons is that JavaScript is always involved.)

To handle XML, .NET FCL offers different possibilities. This is my short overview:
  1. Use System.Xml.XmlDocument class. It implements DOM interface; this way is the easiest and good enough if the size if the document is not too big.
  2. Use the classes System.Xml.XmlTextWriter and System.Xml.XmlTextReader; this is the fastest way of reading, especially is you need to skip some data.
  3. Use the class System.Xml.Linq.XDocument; this is the most adequate way similar to that of XmlDocument, supporting LINQ to XML Programming.
Good luck.
Maciej Los 17-May-16 2:22am
Well, a 5!
Thank you, Maciej.

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