XML 문자열을 deserialize하려면 tring 해요,하지만 어떤 이유로 제목에 명시된 오류가 나타납니다.XmlException : 문서 요소가 나타나지 않았습니다 - 줄 1, 위치 1
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);
}
}
당신은 내가 심지어 공백을 제거하려 주석 라인에서 볼 수 있듯이,하지만 그 eather 작동하지 않았다 :
이
내가에서 역 직렬화하고있어 코드입니다. 내 플레이어 클래스 여기recieveObject<Player>(ref player);
그리고있다 :
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;
}
}
나는으로 직렬화하는 데 사용할 tryng있어 마지막으로 XML을
이
내 코드에서 recieveObject 함수에 대한 호출입니다 선수 개체 : 내가 제목에 오류를 받고 있어요 왜<player>
<x>50</x>
<y>100</y>
<name>Tester</name>
</player>
누군가가 말해 주시겠습니까?
감사합니다.
당신이 받고있는 것 같다 데이터는 UDP 채널을 통해 전송됩니다. 0 바이트보다 많이 수신한다고해서 반드시 데이터가 완료되었다는 의미는 아닙니다. 잘못된 XML 일 수 있습니다. 그렇지 않으면 나는 Adriano의 대답에 동의한다. –