views:

25

answers:

2

Hey,

I am trying to parse some XML in AS3 that I recieve thru a WebService call to C#. C# is serializing using a DataContract so the namespace is non standard.

Here is what the xml looks like:

<User xmlns="http://schemas.datacontract.org/2004/07/UserDatabaseManipulation.POCO" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"&gt;
  <Email>
    <EmailString>
      [email protected]
    </EmailString>
  </Email>
  <Password>
    <PasswordPlainText>
      password
    </PasswordPlainText>
  </Password>
  <ReferralDetails>
    <ReferralEmail/>
    <ServiceCreatedAt>
      google
    </ServiceCreatedAt>    
  </ReferralDetails>
  <UserDetails>
    <Address>
      Penn Ave
    </Address>
    <City>
      Washington DC
    </City>
    <Country>
      USA
    </Country>
    <FirstName>
      Bill
    </FirstName>
    <LastName>
      Clinton
    </LastName>
    <State>
      AK
    </State>
    <Zip>
      11111
    </Zip>
  </UserDetails>
</User>

So as you can see from that I have a User which consists of Email, Password, Referral Details, and UserDetails.

Here is where I parse it and the problem:

private function onResult(event:ResultEvent):void
        {           
            var n:Namespace = new Namespace("http://schemas.datacontract.org/2004/07/UserDatabaseManipulation.POCO");
            use namespace n;                    

//This WORKS! ResultXml is loaded with the correct looking XML.
            var resultXml:XML = new XML(event.result);  

//This doesnt work! I just end up with an empty XMLList.
            var email:Object = resultXml.Email;

...

Here's a screen shot in debug view (copy link and re-view to see it bigger):

alt text

Without e4x I can get it to work like this but it is really clunky:

var resultXml:XML = new XML(event.result);   // the whole block of XML

            var email:XML = resultXml.children()[0]; // the email object XML

            var emailText:XML = email.children()[0]; // the email text

            var emailActualXml:XML = emailText.children()[0]; // the email string in xml

            var emailString:String = emailActualXml.toString(); 

Screenshot:

alt text

HERES THE SOLUTION

var xmlNamespace:Namespace = new Namespace( // namespace in here );         

            var resultXml:XML = new XML(event.result);          

            var email:XMLList = resultXml.xmlNamespace::Email;

            var emailString:Object = email.xmlNamespace::EmailString.text().toString();
A: 

Try this:

var email:XMLList = resultXml..Email;

//access the user email
var userEmail:String = String[email.EmailString];

Of course you could access EmailString directly with the dot syntax!

PatrickS
nope. no good :( I posted a screen shot showing this not working.
nextgenneo
+2  A: 

You must use the fully qualified name (including the namespace) when there are namespaces involved.

var n:Namespace = new Namespace("http://schemas.datacontract.org/2004/07/UserDatabaseManipulation.POCO");
var resultXml:XML = new XML(event.result);  
var email:Object = resultXml.n::Email;

Or use the default xml namespace directive

default xml namespace = new Namespace("http://schemas.datacontract.org/2004/07/UserDatabaseManipulation.POCO");

var resultXml:XML = new XML(event.result);  
var email:Object = resultXml.Email;
Amarghosh
great works like a charm!
nextgenneo