How about you just save the xml to a file, and use xsd to generate C# classes?
- Write the file to disk (I named it foo.xml)
- Generate the xsd:
xsd foo.xml
- Generate the C#:
xsd foo.xsd /classes
Et voila - and C# code file that should be able to read the data via XmlSerializer
:
XmlSerializer ser = new XmlSerializer(typeof(Cars));
Cars cars;
using (XmlReader reader = XmlReader.Create(path))
{
cars = (Cars) ser.Deserialize(reader);
}
(include the generated foo.cs in the project)
If you use an appropriate class or library, they will do the escaping for you. Many XML issues are caused by string concatenation.
XML escape characters
There are only five:
" "
' '
< <
> >
& &
Escaping characters depends on where the special character is used.
The examples can be validated at the W3C Markup Validation Service.
Text
The safe way is to escape all five characters in text. However, the three characters "
, '
and >
needn't be escaped in text:
<?xml version="1.0"?>
<valid>"'></valid>
Attributes
The safe way is to escape all five characters in attributes. However, the >
character needn't be escaped in attributes:
<?xml version="1.0"?>
<valid attribute=">"/>
The '
character needn't be escaped in attributes if the quotes are "
:
<?xml version="1.0"?>
<valid attribute="'"/>
Likewise, the "
needn't be escaped in attributes if the quotes are '
:
<?xml version="1.0"?>
<valid attribute='"'/>
Comments
All five special characters must not be escaped in comments:
<?xml version="1.0"?>
<valid>
<!-- "'<>& -->
</valid>
CDATA
All five special characters must not be escaped in CDATA sections:
<?xml version="1.0"?>
<valid>
<![CDATA["'<>&]]>
</valid>
Processing instructions
All five special characters must not be escaped in XML processing instructions:
<?xml version="1.0"?>
<?process <"'&> ?>
<valid/>
XML vs. HTML
HTML has its own set of escape codes which cover a lot more characters.
Best Answer
In order to have Flex automatically serialize and deserialize objects for your, there needs to be some kind of WSDL or RPC Protocol. So you either need to specify a WSDL to use to deserialize XML data, use AMF or some other protocol, or work with whatever MIME type your httpservice returns directly.
EDIT: You could set up your own set of classes to handle the deserialization of your xml objects. The way the AMF service works is it first deserialzes the returned objects into mx.utils.ObjecProxy instances, then matches the
type
attribute of theObjectProxy
to an existingRemoteClass
and initializes the properties of the class in thePropertyList
to values in the dynamicObject
property. You could set your own set of classes that create ObjectProxy's by looking at the XML root for the type and child nodes for the properties and values, then instantiate your AS classes based on the Object proxies. You aren't going to be able to get around having to write some code to directly handle the XML though.