网上关于XML的增删改查的内容讲解的都比较清楚了,自己也是记录下来常用的方法方便于自己使用,所以内容比较简陋,仅供参考。
1. 直接对XML进行读取;
/// <summary>
/// xml文件的生成
/// </summary>
public void CreateXmlFile()
{
// 创建XmlDocument对象
XmlDocument xmlDoc = new XmlDocument();
// 加入XML声明
XmlDeclaration xmldec= xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null);
xmlDoc.AppendChild(xmldec);
// 创建一个根元素
XmlElement xmlj = xmlDoc.CreateElement("Time");
xmlDoc.AppendChild(xmlj);
// 创建子元素
XmlElement xmlB = xmlDoc.CreateElement("MqttConfig");
xmlB.SetAttribute("waitT", "1.5");
xmlB.SetAttribute("moveT","5");
xmlj.AppendChild(xmlB);
// 保存XML文件
xmlDoc.Save(Application.streamingAssetsPath+"/TextXML.xml");
}
/// <summary>
/// xml文件的读取
/// </summary>
public void GetXmlInfo()
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(xmlPath);
//XmlNode xmlnode= xmlDoc.SelectSingleNode("Time");
//Debug.Log(xmlnode.InnerXml);
//Debug.Log(xmlnode.ChildNodes.Count);
//for (int i = 0; i < xmlnode.ChildNodes.Count; i++)
//{
// Debug.Log(xmlnode.ChildNodes[i].Name);
// Debug.Log(xmlnode.ChildNodes[i].Value);
// Debug.Log(xmlnode.ChildNodes[i].InnerXml);
// Debug.Log(xmlnode.ChildNodes[i].InnerText);
//}
//XmlElement xxee = xmlDoc.DocumentElement;
//foreach (XmlElement node in xxee)
//{
// Debug.Log(node.GetAttribute("serverIP"));
//}
//Debug.Log(xxee.ChildNodes.Count);
XmlElement xmlE =xmlDoc.DocumentElement;
foreach (XmlNode node in xmlE.ChildNodes)
{
if (node.Name.Equals("Time"))
{
Debug.Log(node.Attributes["waitT"].Value);
Debug.Log(node.Attributes["moveT"].Value);
XmlNode xx = node.FirstChild;
Debug.Log(xx.Attributes["serverIP"].Value);
}
}
//直接查找某个节点的xml内容
XmlNode xmlC = xmlDoc.SelectSingleNode("Time/MqttConfig");
Debug.Log(xmlC.Attributes["serverIP"].Value);
}
/// <summary>
/// 直接查找某个节点的xml内容
/// </summary>
/// <param name="filePath"></param>
/// <param name="fileNode"></param>
/// <returns></returns>
public static string GetIPPort(string filePath, string fileNode)
{
//创建xml文档
XmlDocument xml = new XmlDocument();
XmlReaderSettings set = new XmlReaderSettings();
//这个设置是忽略xml注释文档的影响。有时候注释会影响到xml的读取
set.IgnoreComments = true;
XmlReader reader = XmlReader.Create(filePath, set);
xml.Load(reader);
reader.Close();
XmlNode xmlNode = xml.SelectSingleNode(fileNode);
return xmlNode.InnerText;
}
<?xml version="1.0" encoding="UTF-8"?>
<Time waitT="1.5" moveT="5">
<MqttConfig serverIP="192.168.13.143">
</MqttConfig>
</Time>
2. 通过类的方式序列化读取;
如何读取xml调用方法
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ReadXmlTest : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
ModelData MD = XmlTool.initialize().DeserializeMethod<ModelData>(Application.dataPath + "/XMLTest/xinxi.xml");
}
}
读取xml方法类
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;
using UnityEngine;
public class XmlTool
{
public static XmlTool initialize()
{
if (xmlTool == null)
{
xmlTool = new XmlTool();
}
return xmlTool;
}
private XmlTool() { }
private static XmlTool xmlTool;
/// <summary>
/// 序列化存储xml
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="t"></param>
/// <param name="filePath"></param>
public void SerializeMethod<T>(T t, string filePath)
{
Debug.Log(filePath);
//if (File.Exists(filePath))
//{
StreamWriter writer = new StreamWriter(filePath);
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", "");//把命名空间设置为空,这样就没有命名空间了
XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
xmlSerializer.Serialize(writer, t, ns);
writer.Close();
//}
}
/// <summary>
/// 反序列化读取xml
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="filePath"></param>
/// <returns></returns>
public T DeserializeMethod<T>(string filePath)
{
T t = default(T);
if (File.Exists(filePath))
{
StreamReader reader = new StreamReader(filePath);
XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
t = (T)xmlSerializer.Deserialize(reader);
reader.Close();
}
return t;
}
}
程序发布后读取其他路径的xml
private string newxmlpath;
void ReadSateXML()
{
switch (Application.platform)
{
case RuntimePlatform.WindowsEditor://PC端
newxmlpath = Application.dataPath + "/MyProject/Resources/XML/Config.xml";
break;
case RuntimePlatform.WindowsPlayer:
string xmlpath = Application.dataPath;
int num = xmlpath.LastIndexOf("/");
xmlpath = xmlpath.Substring(0, num);
//int num2 = xmlpath.LastIndexOf("/");
//xmlpath = xmlpath.Substring(0, num2);
newxmlpath = xmlpath + "/XML/Config.xml";
break;
}
XmlSerializer serializer = new XmlSerializer(typeof(ModelData));
TextReader textreader = new StreamReader(newxmlpath);
ModelData sateinfoxml = serializer.Deserialize(textreader) as ModelData;
// text.text = newxmlpath;
string m_IP = sateinfoxml.areaInfoList[0].Name;
Debug.Log("连接IP========" + m_IP);
}
定义类文件
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Xml.Serialization;
public class ModelData
{
[XmlElement("AreaInfo")]
public List<AreaInfo> areaInfoList;
}
public class AreaInfo
{
[XmlAttribute]
public string Name;
[XmlElement("ModelInfo")]
public List<ModelInfo> modelInfoList;
}
public class ModelInfo
{
[XmlAttribute]
public string UID;
[XmlAttribute]
public string ReceiveID;
[XmlAttribute]
public string Type;
[XmlAttribute]
public string Area;
[XmlElement("DetailedInfo")]
public List<DetailedInfo> detailedInfoList;
}
public class DetailedInfo
{
[XmlAttribute]
public string Key;
[XmlAttribute]
public string Value;
}
xml文件
<?xml version="1.0" encoding="UTF-8"?>
<ModelData>
<AreaInfo Name="区域信息">
<ModelInfo UID="000" ReceiveID="111" Type="222" Area="333">
<DetailedInfo Key="000" Value="000"/>
</ModelInfo>
</AreaInfo>
</ModelData>
1237



被折叠的 条评论
为什么被折叠?



