0

From my backend I am consuming a web service that returns the following:

<!-- this is the info of the user --> \r\n<User>\r\n<Name>Jhon Doe</Name>\r\n<Identification>10538181</Identification>\r\n<Email>JhonDoe@gmail.com</Email></User>  

I want to extract the text contained in these tags: <Name> <Identification> <Email> since I am new to c# I want to know which is the best way to achieve it

This is my desired output:

output:

{
"Name":"Jhon Doe",
"Identification":10538181,
"Email":"JhonDoe@gmail.com",
}

how can do it?

yavg
  • 2,761
  • 7
  • 45
  • 115
  • 3
    You parse XML (it looks like one) to a class and then serialize this class to json. – Guru Stron Nov 06 '20 at 14:43
  • 1
    Sometimes you can request the data in xml or json format. Check to see if you add a accept: application/json header if you can get it in Json format directly – Richard Hubley Nov 06 '20 at 14:52
  • To anyone who clicked "Close": the selected dupe covers the XML parsing, but NOT the conversion from XML(-object/node) to JSON. – Peter B Nov 06 '20 at 15:03

1 Answers1

0

Use this:

string xml = "YOUR INPUT";

XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);

string json = JsonConvert.SerializeXmlNode(doc);
llouk
  • 513
  • 4
  • 15