C# 为什么XmlTextReader在统一中变得如此缓慢?
我需要解析一个大的XML文件(≈100MB,400000行),然后将数据放入列表中C# 为什么XmlTextReader在统一中变得如此缓慢?,c#,xml,unity3d,large-files,u3d,C#,Xml,Unity3d,Large Files,U3d,我需要解析一个大的XML文件(≈100MB,400000行),然后将数据放入列表中 首先,我尝试在一个C#console应用程序中解析XML文件,大约需要2s才能完成这项工作 然后我将代码复制到Unity中,大约需要17秒才能完成这项工作 有人知道为什么会变得这么慢吗?如何使它更快? 谢谢 代码: // Used for storing data stateList = new List<BallisticState>(); XmlTextReader reader = new
- 首先,我尝试在一个C#console应用程序中解析XML文件,大约需要2s才能完成这项工作
- 然后我将代码复制到Unity中,大约需要17秒才能完成这项工作
// Used for storing data
stateList = new List<BallisticState>();
XmlTextReader reader = new XmlTextReader(filepath);
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
if (reader.Name == "row")
{
string value = reader.GetAttribute("value").TrimStart();
BallisticState state = new BallisticState();
// this method converts string to float
SetBallisticState(state, value);
stateList.Add(state);
}
}
}
//用于存储数据
stateList=新列表();
XmlTextReader=新的XmlTextReader(文件路径);
while(reader.Read())
{
if(reader.NodeType==XmlNodeType.Element)
{
如果(reader.Name==“行”)
{
字符串值=reader.GetAttribute(“值”).TrimStart();
弹道状态=新弹道状态();
//此方法将字符串转换为浮点
setballistate(状态、值);
stateList.Add(state);
}
}
}
我们无法在此处访问您的XML文件,因此您必须帮助我们确定您的代码的哪部分速度非常慢。可能是新的XmlTextReader(filepath),reader.Read()
或while
循环中的代码。请把我在Console和Unity应用程序中提到的这三件事告诉我,然后用每件事的结果更新你的问题。发布计时代码也很好,这样我们就知道你做得对。XML是数据的正确结构吗?