unity Project Gets and Changes xml Configuration

Recently, when working on a pc project, it was required to modify the relevant xml configuration in the running application and save it.
As shown in the figure:


Project Root Directory and xml Hierarchy Diagram
xml data:
So how to modify the xml file outside the project and save the modification? Don't talk too much about code

1. Get xml-related data by deserialization first

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;


[XmlType(TypeName = "SystemSetting")]
public class SystemSetting
{
    
    [XmlElement]
    public bool IsDebug = false;
    /// <summary>
    /// Screen resolution
    /// </summary>
    [XmlElement]
    public  ResolutionSetting ResolutionSetting; 
    /*todo:
     *     1.Add a page to display the list of logs, which is displayed when IsDebug is true.
     *     2.Add a location to show whether the current visitor mode or management mode is
     */

    /// <summary>
    /// Version information, just one version number
    /// </summary>
    [XmlElement]
    public VersionSetting VersionSetting;

    /// <summary>
    /// Whether to Force Full Screen
    /// </summary>
    [XmlElement]
    public bool IsFullScreen ;

    /// <summary>
    /// Whether to Display Home Page
    /// </summary>
    [XmlElement]
    public bool IsShowHomePage=true ;

    /// <summary>
    /// Does the left-hand extension map appear?
    /// </summary>
    [XmlElement]
    public bool IsShowLeftTopo=true;

    /// <summary>
    /// Whether to display alarm push in lower right corner
    /// </summary>
    [XmlElement]
    public bool IsShowRightDownAlarm=true;

    /// <summary>
    /// Whether to display statistics in the upper right corner
    /// </summary>
    [XmlElement]
    public bool IsShowRightTopInfo=true;

    /// <summary>
    /// Camera Following Related Parameters
    /// [Subsequent functional adjustments will be useless]
    /// </summary>
    [XmlElement]
    public CinemachineSetting CinemachineSetting;

    /// <summary>
    /// Communication-related parameter settings
    /// </summary>
    [XmlElement]
    public CommunicationSetting CommunicationSetting;

    /// <summary>
    /// refresh interval
    /// 1. Real-time location information
    /// 2. Regional statistical data
    /// 3. Personnel Tree Node
    /// 4. Departmental tree node refresh
    /// 5. Renewal of Camera Interface Nearby
    /// 6. Top view screenshots are saved. [It's no use, put it first, you can set it up]
    /// </summary>
    [XmlElement]
    public RefreshSetting RefreshSetting;

    /// <summary>
    /// Honeywell Video Settings
    /// </summary>
    [XmlElement]
    public HoneyWellSetting HoneyWellSetting;

    /// <summary>
    /// Model dynamic loading related settings
    /// </summary>
    [XmlElement]
    public AssetLoadSetting AssetLoadSetting;

    /// <summary>
    /// History Track Relevant Settings
    /// </summary>
    [XmlElement]
    public HistoryPathSetting HistoryPathSetting; 

    /// <summary>
    /// Location-related settings
    /// </summary>
    [XmlElement]
    public LocationSetting LocationSetting;

    /// <summary>
    /// Debugging mode related settings
    /// </summary>
    [XmlElement]
    public DebugSetting DebugSetting;

    /// <summary>
    /// Device Loading Related Settings
    /// [Temporary debugging, no display]
    /// </summary>
    [XmlElement]
    public DeviceSetting DeviceSetting;
    /// <summary>
    /// Whether to display alarm or not
    /// </summary>
    [XmlElement]
    public AlarmSetting AlarmSetting;

    public SystemSetting()
    {
        ResolutionSetting = new ResolutionSetting();
        CinemachineSetting = new CinemachineSetting();
        CommunicationSetting = new CommunicationSetting();
        VersionSetting = new VersionSetting();
        RefreshSetting = new RefreshSetting();
        AssetLoadSetting = new AssetLoadSetting();
        HoneyWellSetting = new HoneyWellSetting();
        DeviceSetting = new DeviceSetting();
        HistoryPathSetting = new HistoryPathSetting();
        LocationSetting = new LocationSetting();
        DebugSetting = new DebugSetting();
        AlarmSetting = new AlarmSetting();
    }
}
2.modify xml Relevant data

Now that you have all the corresponding objects and attributes, you can modify them and do a simple example.

SystemSettingHelper.systemSetting.IsShowLeftTopo = false;
IsShow Left Topo is one of the data in xml, and other data can be modified in the same way.
3. Save Modified Contents
/// <summary>
    /// Save system settings
    /// </summary>
    public static void SaveSystemSetting()
    {
        string path = Application.dataPath + ConfigPath;
        try
        {
            SerializeHelper.Save(systemSetting, path);
        }
        catch (Exception ex)
        {
            Debug.LogError(ex.ToString());
        }

    }
    //serialize

        /// <summary>
        /// Save objects to files
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="fileName"></param>
        public static void Save(object obj, string fileName)
        {
            //Serialize(obj, fileName);
            Save(obj, fileName, Encoding.UTF8);
        }

        /// <summary>
        /// Save objects to files
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="fileName"></param>
        /// <param name="encoding"></param>
        public static void Save(object obj, string fileName, Encoding encoding)
        {
            CreateFileSDirectory(fileName);
            string text = GetXmlText(obj, encoding);
            File.WriteAllText(fileName, text, encoding);
        }
        //The front and back effects are shown in the figure.



So far, we have completed all the steps of getting xml data modification and saving.

Keywords: xml encoding

Added by netizen on Wed, 31 Jul 2019 12:01:22 +0300