XmlException:文档元素未出现-行1,位置1 [英] XmlException: Document element did not appear - line 1, position 1
问题描述
我正试图反序列化xml字符串,但是由于某种原因,我得到了标题中指出的错误。
I'm tring to deserialize a xml string, but for some reason I'm getting the error stated in the title.
这是我要的代码反序列化自:
This is the code I'm deserializing from:
public void recieveObject<T>(ref T t){
XmlSerializer xs = new XmlSerializer(typeof(T));
Debug.Log("Waiting for client");
byte[] recievedData = udpc.Receive(ref recieveFromEndPoint);
if(recievedData.Length > 0){
string xmlStr = System.Text.Encoding.UTF8.GetString(recievedData, 0, recievedData.Length);
//xmlStr = xmlStr.Replace("\r","").Replace("\n", "").Replace("\t","").Replace(" ", "");
Debug.Log(xmlStr);
MemoryStream rms = new MemoryStream(1024);
rms.Write (System.Text.Encoding.UTF8.GetBytes(xmlStr), 0, System.Text.Encoding.UTF8.GetBytes(xmlStr).Length);
Debug.Log ("ms: " + System.Text.Encoding.UTF8.GetString(rms.ToArray()));
t = (T) xs.Deserialize(rms);
}
}
从注释行可以看到,我什至尝试过
As you can see from the commented line I even tried stripping out the white space, but that didn't work eather.
这是我代码中对recieveObject函数的调用:
This is the call to the recieveObject function in my code:
recieveObject<Player>(ref player);
这是我的Player类:
And here is my Player class:
using UnityEngine;
using System.Collections;
using System.Xml.Serialization;
[XmlRoot("player")]
public class Player{
[XmlElement("x")]
public int x;
[XmlElement("y")]
public int y;
[XmlElement("name")]
public string name;
private int maxNameLength = 12;
public Player(){}
public Player(int x, int y, string name){
this.x = x;
this.y = y;
if(name.Length > maxNameLength) name = name.Substring(0, maxNameLength);
this.name = name;
}
}
最后是我尝试使用的Xml反序列化为播放器对象:
and finally the Xml I'm tryng to use to deserialize into a player object:
<player>
<x>50</x>
<y>100</y>
<name>Tester</name>
</player>
有人可以告诉我为什么标题出现错误吗?
Can someone please tell me why I'm getting the error in the title?
谢谢您的时间。
推荐答案
您正在从内存流末读取:
You're reading from end of memory stream:
MemoryStream rms = new MemoryStream(1024);
rms.Write (...);
// Now cursor is at end of file, nothing to read here
t = (T) xs.Deserialize(rms);
在反序列化之前只需将光标移回开头:
Just move cursor back to beginning before you deserialize:
rms.Seek(0, SeekOrigin.Begin);
t = (T) xs.Deserialize(rms); // Now deserializer has data to read
的数据,最后只有两个小建议。不要忘记处理所有一次性对象:
Finally just two small suggestions. Don't forget to dispose all disposable objects:
MemoryStream rms = new MemoryStream(1024);
{
}
此外,您无需读取字节流成字符串(解码UTF8),然后又返回字节(来自UTF8),这种双重转换不会增加任何内容(此外,请注意两次编码,因为您两次调用 GetBytes()
) :
Also you don't need to read a byte of stream into a string (decoding UTF8) then getting bytes back (from UTF8), this double conversion adds nothing (moreover please note you encode twice because you call GetBytes()
twice):
if (recievedData.Length > 0)
{
using (MemoryStream rms = new MemoryStream(receivedData))
{
t = (T) xs.Deserialize(rms);
}
}
出于记录目的,您可以编写如下函数( UTF8转换将仅在必要时进行):
For logging purposes you can write a function like this (UTF8 conversion will be done only if necessary):
static class Logger
{
[Conditional("DEBUG")]
public static void Debug(Func<string> text)
{
Debug.Log(text());
}
}
您的日志记录将会(它将被称为仅在定义了 DEBUG
符号的情况下):
Your logging will be (and it'll be called only if DEBUG
symbol is defined):
Logger.Debug(() => "ms: " + Encoding.UTF8.GetString(rms.ToArray()));
这是一个更漂亮的选择:
It's just a more pretty alternative to:
#if DEBUG
Debug.Log ("ms: " + System.Text.Encoding.UTF8.GetString(rms.ToArray()));
#endif
这篇关于XmlException:文档元素未出现-行1,位置1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!