- Home /
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.
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.
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.
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.
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.
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
Follow this Question
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