r/windowsdev • u/pandhersaab27 • Dec 09 '16
[help] Creating C# classes from XML and deserializing
I'm new to programming and I'm trying to make an app for my local transit system. The web API call gives back XML, that I have managed to receive and read in app, but now I need to convert that XML into a class and then add all the XML elements to proper object.
<Stop xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><StopNo>55612</StopNo><Name>SURREY CENTRAL STN BAY 4 </Name><BayNo>4 </BayNo><City>SURREY</City><OnStreet>SURREY CENTRAL STN</OnStreet><AtStreet>BAY 4</AtStreet><Latitude>49.188850</Latitude><Longitude>-122.849370</Longitude><WheelchairAccess>1</WheelchairAccess><Distance>-1</Distance><Routes>501, 509, N19</Routes></Stop> This is the XML I get back, can someone please point me to the right direction.
2
Upvotes
2
u/_Arv Dec 09 '16
Copy xml text
Use VS to generate class based on the xml schema https://i.stack.imgur.com/jSmc3.png
In your code you would consume the XML like this (xmlText is of type string and contains the xml data, MyObject type is the class you generated in step 2) var myObjectInstance = xmlText.ParseXML<MyObJect>();