Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 ChrisSch · Feb 08, 2015 at 01:12 PM · c#xmlreadxmlserializerwrite data

Xml Serialization? (how to read/write?)

I finally decided to stop being scared of xml and just learn it already and stop avoiding it. And now I'm so confused. I've been through dozens of google/forums/youtube searches and I can not understand this XML serialization. Its all alien language to me.

First it took me a bit to realize that there's a few ways to go about reading and writing in xml files. There's that XmlDocument thing, and theres the serialization, and probably something else too. If I understood it correctly, those two things are different ways, and serializing is better. I sort of understood the document way, but serialization is confusing me even more.

Now I've been to the unify community tutorial about it and I don't get it. How exactly are you reading or writing anything? I just don't see it happening in the code. And why do I need more than one script? I can't find a simple example at all on how to do this. Or I'm just too stupid for this.

I wanted to learn by making a simple checklist program, and saving/loading the data from an xml file.

Here's the short version:

 <Checklists>
     <Checklist_1>
         <Day_1>
             <Thing_1>example 1</Thing_1>
             <Thing_2>example 2</Thing_2>
         </Day_1>
         <Day_2>
             <Thing_1>example 1</Thing_1>
             <Thing_2>example 2</Thing_2>
         </Day_2>
     </Checklist_1>
     <Checklist_2>
         <Day_1>
             <Thing_1>example 1</Thing_1>
             <Thing_2>example 2</Thing_2>
         </Day_1>
         <Day_2>
             <Thing_1>example 1</Thing_1>
             <Thing_2>example 2</Thing_2>
         </Day_2>
     </Checklist_2>
 </Checklists>

Now what to do in actual code? It got confusing real fast because this turns out to be an array within an array within an array.

This is as far as I got:

 [XmlRoot("Checklists")]
 public class Checklists
 {
     [XmlArray("Checklist_1")]
     [XmlArrayItem("Day_1")]
     public List<Day_1> Checklist_1 = new List<Day_1>();
 }
 
 public class Day_1
 {
     public string Thing_1;
     public string Thing_2;
 }

Yeah I know... its sad. Now if this is correct at all, I don't understand the reading and writing part in the tutorial. I don't see any variables like health being assigned or read there. Is it because of the [XmlRoot("Checklists")] part? The vars and the xml file are being connected? And then the read write is just syncing everything from and to? What if you just wanted to sync one var? And how is then the public class Day_1 being connected with the file? Because its being declared in Checklists class? Or am I completely off on this?

Thank you.

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Wiki

Answer by Elenesski · Mar 20, 2015 at 08:03 PM

You could use an XDocument, this is a LINQ enabled XML parser to read XML documents or fragments. I'd really recommend that you not number the elements in your XML, as the order is implied by the sequence of elements in your XML.

 StringReader SR = new StringReader(File.ReadAllText(XMLFile));        // or just some text text
 XDocument DOC = XDocument.Load(SR);
 foreach (XElement NODE in DOC.Nodes().OfType<XElement>().Where(A => A.Name == "Checklists")) {
     foreach (XElement CHECKLIST in NODE.OfType<XElement>().Where(A => A.Name.ToString().StartsWith() == "CheckList_" ) ) {
         // and so on
     }
 }

Call me crazy, but for writing, I tend to actually write my XML out with a StringBuilder and construct the XML on the fly. I found that the XML document libraries tended to be slower and gave me a bunch of other things that weren't necessary for most of my purposes.

I also recommend creating XElement extensions, to wrap things like the Where clauses, because these statements get to be really hard to read.

     public static IEnumerable<XElement> GetAll(this XElement aNode, string aName) {
         return aNode.Nodes().OfType<XElement>().Where(A => A.Name == aName );

     }

and

     public static IEnumerable<XElement> StartsWith(this XElement aNode, string aName) {
         return aNode.Nodes().OfType<XElement>().Where(A =>  A.Name.ToString().StartsWith(aName));

     }

So that you can code things like

 foreach (XElement CHECKLIST in NODE.StartsWith("CheckList_") { ... }

To make it much easier to read and understand.

Comment
Add comment · Share
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

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

20 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

Related Questions

Read AND Write to XML at runtime 1 Answer

Loading Xml file issues 0 Answers

Reading level specific file in build (from another level) 0 Answers

Problems with the XML Serializer 1 Answer

Need Help With XML Reading Class Initialization 1 Answer


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