- Home /
unity and xml question
Ok firstly let me break down what I am trying to achieve.
Im using a photon2 setup where the room is created and run in a headless mode so It basically starts the lobby and room with no players. other players can then join from a list of all availiable rooms.
Because im running a headless version I would like the person who starts the (Server/lobby/room) to be able to make a custom name and info for the room that the other players will find in the list.
This works in the normal build by the owner declaring the name and stuff from unities UI. But because I will ship a headless version I would like all the options inc. name, to be sorted out from an xml doc.
This is the basic xml setup.
<?xml version="1.0" encoding="utf-8"?>
<ServerSettings>
<Settings>
<serverName>test server of doom</serverName>
<Max_Players>100</Max_Players>
<Game_Mode>pvp/pve</Game_Mode>
</Settings>
</ServerSettings>
Now because this will be defined by the player I have the .xml in the StreamingAssets folder. I have done this so the person with the headless build can find the file to change it. And this is where I am coming unstuck.
I need to know how to get the data from the xml from streaming assets and convert it to the roomName variable.
This is what I have but im getting stuck... any help and I will love you forever:
public void GetserverDetails(string xmlData)
{
XmlDocument mlcDoc = new XmlDocument();
mlcDoc.Load(new StringReader(xmlData));
string pathPatthern = Application.dataPath + "/StreamingAssets/serversettings.xml";
XmlNodeList xmlnode = mlcDoc.SelectNodes(pathPatthern);
foreach(XmlNode node in xmlnode)
{
XmlNode serverName = node.FirstChild;
roomName = serverName.ToString();
}
}
current code gives this error:
XmlException: Document element did not appear. Line 1, position 1.
Mono.Xml2.XmlTextReader.Read ()
System.Xml.XmlTextReader.Read ()
Mono.Xml.EntityResolvingXmlReader.Read ()
Mono.Xml.DTDValidatingReader.ReadContent ()
Answer by magicbananna · Jan 06, 2019 at 09:15 AM
So I finally figgured it out so If anyone in the future needs this:
void Start () {
string data = Application.dataPath + "//StreamingAssets/serversettings.xml";
PhotonNetwork.GameVersion = _gameVersion;
PhotonNetwork.ConnectUsingSettings();
Debug.Log("called");
GetserverDetails(data);
}
private void Awake()
{
instance = this;
}
public void GetserverDetails(string xmlData)
{
XmlDocument mlcDoc = new XmlDocument();
mlcDoc.Load(xmlData);
string pathPatthern = "//ServerSettings";
XmlNodeList xmlnode = mlcDoc.SelectNodes(pathPatthern);
foreach(XmlNode node in xmlnode)
{
XmlNode serverName = node.FirstChild;
roomName = serverName.InnerXml.ToString();
}
}