2

Possible Duplicate:
XML parsing of a variable string in JavaScript

As part of a server-client project I have an object in a C#-server app which is serialized to an XML string. The string looks like this:

<?xml version="1.0" encoding="utf-8"?>
<CandyShop xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <CandyList>
   <CandyDesc>
     <Taste>Taste1</Taste>
     <Color>Color1</Color>
   </CandyDesc>
   <CandyDesc>
     <Taste>Taste2</Taste>
     <Color>Color2</Color>
   </CandyDesc>
  </CandyList>
  <!-- Other stuff -->
</CandyShop>

I then transfer this XML string via websockets to a javascript-based HTML client. I'd like to deserialize the initial class so that I could just type

var aColor = CandyShop.CandyList.CandyDesc[0].Color;

For JSON strings I just use

JSON.parse(stringToParse);

Is there an equivalent for XML?

Bonus if I can pre-create a CandyShop class and map the fields directly.

Community
  • 1
  • 1
malber
  • 1,053
  • 4
  • 16
  • 24

1 Answers1

0

There is no direct equivalent to JSON.parse for dealing with XML, this is because what XML does is not directly equivalent to what JSON does so reading it in this way doesn't make sense in general. Read the link T. J. Crowder provides in the comment above for details on how to carry out XML parsing.

Jack Aidley
  • 19,439
  • 7
  • 43
  • 70