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
2
Question by Xenland · May 11, 2011 at 08:39 AM · networkingstringxmlparser

Simple XML reading? (only string/numerical data needed)

I know this has been asked before(most of them only demonstrate with texture/resource downloads), there's scripts for this thing(none of them have documentation on how to use them or a brief description of limitations). I'm working on an MMO so my plan is to send the positions/rotation/animation data with wwwForms, then receive it from an XML file(since I still can't figure out how do use string variables instead; I'd rather go that route) and then take the XML data and update each individual players.

All I really need is a brief introduction on accessing nodes and there values in XML files the rest I can take from there with my Actionscripting3.0 and XML background.

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
4
Best Answer

Answer by CHPedersen · May 11, 2011 at 08:53 AM

Try looking around real quick first. :) I answered a question almost identical to this one yesterday, and you can find hundreds of posts by doing a simple google for this common task.

You need to use the System.xml namespace from .Net. The articles on MSDN on how to use them are pretty self explanatory. If your source is a file, start with XmlDocument.Load.

Edit: Here's some further explanation, with examples:

Since you mentioned having to read the XML data from a file, I take the liberty to assume our situations are somewhat similar in the following. I had to read some XML from a file as well. I wrote a method in a static class I could pass filepaths to, and it would load the XML file into memory so I could work with the nodes. The method below demonstrates some common tasks associated with loading and reading XML:

using System.Xml;

public static class XMLFunctionality {
public static void ReadXML(string filepath) { XmlDocument XMLFile = new XmlDocument();

     // How to read the XML document:
     XMLFile.Load(filepath);

     // How to grab the first node from the file:
     XmlNode first = XMLFile.FirstChild;

     // How to traverse nodes in the file 
     // (this loops over just the declaration and the root, since they are the only
     // two top-level children of the XmlDocument object)
     foreach (XmlNode node in XMLFile.ChildNodes)
     {
     }

     // This is how to use a namespace manager to add a namespace to the file, in
     // this case the default namespace (df)
     XmlNamespaceManager NSMng = new XmlNamespaceManager(XMLFile.NameTable);
     NSMng.AddNamespace("df", XMLFile.DocumentElement.NamespaceURI);

     // This is how to select first all elements by the type TagName, then index
     // into the returned XmlNodeList to pick out the one at index 0:
     XmlNode FirstNodeFound = XMLFile.GetElementsByTagName("TagName")[0];

     // This is how to use a simple XSLT command to select multiple nodes of
     // some particular type. Notice the default namespace prefix:
     XmlNodeList AllNodesOfType = WellNode.SelectNodes("df:yourType", NSMng);
 }

}

I don't really know what else to add. You're right that XmlDocument has a ton of functionality. That's because there's a ton of things you can do with XML. :) You're just going to have to experiment a bit, though the above should get you started.

As a general hint:

When you search for a node or some attribute, never form too complicated search queries right away. Start slow, and build the search up. Start by simply getting it to print the root node. Then dive into that, and print out the first child of the root node... Then see if you can use an element Name to find an arbitrary element among the children of the root node, etc. etc. But always print out intermediate stages so you can keep track of where you are in the document.

Comment
Add comment · Show 5 · 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
avatar image Jean-Fabre · May 11, 2011 at 09:25 AM 0
Share

agree, google is a good friend.

avatar image Xenland · May 11, 2011 at 08:44 PM 0
Share

I'm sorry but I respectfully disagree with the self explanatory part. I've almost have the all of the System.X$$anonymous$$L pages from $$anonymous$$SDN memorized and I still don't fully understand why I can't extract the data with unity correctly. Theres over 100's if not 1000's of events, attributes, etc with all the functions listed on there. Theres just way too many! I've attempted at using the SimpleXmlParser, $$anonymous$$iniParser & both user written xml parser scripts that are on the unity forums. Im having more difficulty with X$$anonymous$$L data then I am with the $$anonymous$$ath functions in this program. Xml shouldnt be this difficult.

avatar image Xenland · May 11, 2011 at 08:53 PM 0
Share

When I say attempted I don't mean "I imported it into unity and when the first error message was thrown my way I cryed to the forums". Im talking about with what knowledge I have about OOP program$$anonymous$$g(C#) I tried hacking some scripts and found that some of the X$$anonymous$$L parsers are hard coded to only read the root tags or . I would like to be apart of this community but its very difficult when half the coders in the community are just as lazy as the ones asking the questions. I wonder why there even is an answer page sometimes.

avatar image CHPedersen · May 12, 2011 at 07:04 AM 0
Share

Alright. I expanded my answer considerably. I hope the provided examples can help you get started, but the best advice I have to offer is to simply experiment yourself.

avatar image Xenland · May 12, 2011 at 09:12 AM 0
Share

Thank you that example is helping out great! I'd like to add that another issue was that $$anonymous$$SDN automatically was sending me to the version 1.1 ins$$anonymous$$d of 3.5 documentation pages; $$anonymous$$aybe that observation will save others victim to this.

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

No one has followed this question yet.

Related Questions

Sync player name (Networking) 1 Answer

Converting String Array to Int. 2 Answers

Run script before run 1 Answer

UnityScript XML parser 0 Answers

web page html parsing 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