0

Приветствую. Пробую написать простое средство для локализации, раньше не работал с xml и json. Пробую так:

    using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Text;
using System.Xml;
using System.IO;

public class LocalizationManager : MonoBehaviour {
    Dictionary<string, string> LangDictionary = new Dictionary<string, string>();
    // Use this for initialization
    void Start () {
        GetLangDictionary("new1.text");

    }

    // Update is called once per frame
    void Update () {

    }
    public void GetLangDictionary(string langFileName)
    {
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.LoadXml(langFileName);
        XmlNodeList wordList = xmlDoc.GetElementsByTagName("object");

        foreach (XmlNode item in wordList)
        {
            LangDictionary.Add(item.Name, item.InnerText);
        }
        Debug.Log(LangDictionary["Cube"]);
    }
}

Однако выдается ошибка

XmlException: Text node cannot appear in this state.  Line 1, position 1.
Mono.Xml2.XmlTextReader.ReadText (Boolean notWhitespace)
Mono.Xml2.XmlTextReader.ReadContent ()
Mono.Xml2.XmlTextReader.Read ()
System.Xml.XmlTextReader.Read ()
System.Xml.XmlDocument.ReadNodeCore (System.Xml.XmlReader reader)
System.Xml.XmlDocument.ReadNode (System.Xml.XmlReader reader)
System.Xml.XmlDocument.Load (System.Xml.XmlReader xmlReader)
System.Xml.XmlDocument.LoadXml (System.String xml)
LocalizationManager.GetLangDictionary (System.String langFileName) (at Assets/Scripts/LocalizationManager.cs:23)
LocalizationManager.Start () (at Assets/Scripts/LocalizationManager.cs:12)

Погуглил, почитал, вроде бы пишут что нужна кодировка UTF-8 без BOM, преобразовал в notepad++ файл в эту кодировку, но изменений никаких. Может я что то делаю не так в самом коде?

Содержимое xml:

<?xml version="1.0"  encoding="UTF-8"?>
<translates>
    <object name="Cube"> Hi, I'm a cube! (xml) </object>
    <object name="Cylinder"> Hi, I'm a Cylinder! (xml) </object>
    <object name="Capsule"> Hi, I'm a Capsule! (xml) </object>
    <object name="Sphere"> Hi, I'm a Sphere! (xml)</object>
</translates>

1 ответ 1

0

Разобрался - вместо LoadXml использовал Load() и записал путь из корневой папки проекта, по умолчанию он его искал там.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Text;
using System.Xml;
using System.IO;

public class LocalizationManager : MonoBehaviour {
    Dictionary<string, string> LangDictionary = new Dictionary<string, string>();
    // Use this for initialization
    void Start () {
        GetLangDictionary("Assets/Scripts/new1.txt");

    }

    // Update is called once per frame
    void Update () {

    }
    public void GetLangDictionary(string langFileName)
    {
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(langFileName);
        XmlNodeList wordList = xmlDoc.GetElementsByTagName("object");

        foreach (XmlNode item in wordList)
        {
            LangDictionary.Add(item.Attributes["name"].Value, item.InnerText);
            Debug.Log("Имя: " + item.Attributes["name"].Value + item.InnerText);
        }
        Debug.Log(LangDictionary["Cube"]);
    }
}

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.