Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by oliver-jones · Apr 06, 2020 at 05:54 PM · xmlserializer

XML into Unity Class - Array and Attributes

Hello, I cannot seem to get my head around on how to deserialise this XML into a class for unity. I've been using the Xml.Serialization, but I cannot seem to get the Attributes correct.

XML;

 <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
     <s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
         <RunSourceResponse xmlns="http://tempuri.org/">
             <RunSourceResult>
                 <Results>
                     <Results>
                         <ProductCode>0022KN</ProductCode>
                         <ProductName>ABC</ProductName>
                         <ProductAvailability>Intermediaries Only</ProductAvailability>
                         <ApplyOnline>false</ApplyOnline>
                         <LenderCode>XYZ</LenderCode>
                         <LenderName>ABC</LenderName>
                     </Results>
                     <Results>
                         <ProductCode>0022KN</ProductCode>
                         <ProductName>ABC</ProductName>
                         <ProductAvailability>Intermediaries Only</ProductAvailability>
                         <ApplyOnline>false</ApplyOnline>
                         <LenderCode>XYZ</LenderCode>
                         <LenderName>ABC</LenderName>
                     </Results>
                     <Results>
                         <ProductCode>0022KN</ProductCode>
                         <ProductName>ABC</ProductName>
                         <ProductAvailability>Intermediaries Only</ProductAvailability>
                         <ApplyOnline>false</ApplyOnline>
                         <LenderCode>XYZ</LenderCode>
                         <LenderName>ABC</LenderName>
                     </Results>
                     <Results>
                         <ProductCode>0022KN</ProductCode>
                         <ProductName>ABC</ProductName>
                         <ProductAvailability>Intermediaries Only</ProductAvailability>
                         <ApplyOnline>false</ApplyOnline>
                         <LenderCode>XYZ</LenderCode>
                         <LenderName>ABC</LenderName>
                     </Results>
                 </Results>
             </RunSourceResult>
         </RunSourceResponse>
     </s:Body>
 </s:Envelope>


This is coming externally from Unity, from a server, and this is how I've made the class, but I'm not getting any results;

 [System.Serializable]
     [XmlRoot("s:Envelope")]
     public class Envelope {
 
         [System.Serializable]
         [XmlRoot("s:Body")]
         public class Body {
 
             [System.Serializable]
             [XmlRoot("RunSourceResponse")]
             public class RunSourceResponse {
 
                 [System.Serializable]
                 [XmlRoot("RunSourceResult")]
                 public class RunSourceResult {
 
                     [System.Serializable]
                     [XmlRoot("Results")]
                     public class Results {
 
 
                         [System.Serializable]
                         //[XmlArray("Results")]
                         public class Result {
 
                             public string ProductCode;
                         }
                     }
                 }
             }
         }
     }


UPDATE

I've updated my structs to this, but still with no luck

 [XmlRoot("s:Envelope")]
     [System.Serializable]
     public struct Envelope {
 
         [XmlAttribute("s:Body")]
         public Body body;
     }
 
     [XmlRoot("s:Body")]
     [System.Serializable]
     public struct Body {
 
         [XmlAttribute("RunSourceResponse")]
         public RunSourceResponse runSourceResponse;
     }
 
     [XmlRoot("RunSourceResponse")]
     [System.Serializable]
     public struct RunSourceResponse {
 
         [XmlAttribute("RunSourceResult")]
         public RunSourceResult runSourceResult;
     }
 
     [XmlRoot("RunSourceResult")]
     [System.Serializable]
     public struct RunSourceResult {
 
         [XmlAttribute("Results")]
         public Results results;
     }
 
     [XmlRoot("Results")]
     [System.Serializable]
     public struct Results {
 
         [XmlElement("Results")]
         public Result[] _result;
     }
 
 
     public Envelope _results;
 
 
     [System.Serializable]
     public struct Result {
 
         public string ProductCode;
         public string ProductName;
         public string ProductAvailability;
         public bool ApplyOnline;
     }

Comment
Add comment · Show 7
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image andrew-lukasik · Apr 06, 2020 at 06:11 PM 0
Share

How does your XmlRoot struct/class looks like?

avatar image andrew-lukasik · Apr 06, 2020 at 06:12 PM 0
Share

This is how you can map main 'Results' array:

 [XmlElement("Results")] public Results[] results_array;

Also remember to flag Results struct (and every other involved) as [System.Serializable].

avatar image andrew-lukasik · Apr 06, 2020 at 06:20 PM 0
Share

Ok, I noticed this sample code just now. So... the thing is - this is not how one mirror xml in c#. Ie. you don't express structure by nesting those types but creating just fields of those types.

avatar image andrew-lukasik · Apr 06, 2020 at 06:22 PM 0
Share

Check this: Wolfram Alpha Xml deserialization sample

avatar image oliver-jones andrew-lukasik · Apr 06, 2020 at 06:46 PM 0
Share

hello, thanks for the help - I've updated my question as I'm still not getting results. I get an error, cannot serialise member 'body'.

avatar image andrew-lukasik oliver-jones · Apr 06, 2020 at 06:56 PM 0
Share

One additional thing, in xml like here:

 <s:Envelope ...>

"s:" is not part of the name but some kind of attribute (i think), so remove it from your [XmlRoot("s:Envelope")] and [XmlRoot("s:Body")]

avatar image andrew-lukasik · Apr 06, 2020 at 06:59 PM 0
Share

Also, paste your xml to online xml2c# converters for quick and dirty first drafts

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

125 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

xmlserializer Serializing/deserializing error 1 Answer

XML Error on Load 1 Answer

Best Way to Store Large Number of GameObjects? 1 Answer

How to type Color32 format into XML? 1 Answer

does xml serializer good to store images and 3d models 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges