我知道这个论点已经面临很多次,但我是一个新手,我不明白如何处理错误。
我是一个Stream XML,我想用数据合同方法对其进行反序列化
你注意到我做错了吗?
这是xml:
<?xml version="1.0"?>
<response>
<sessid>jsh5ekqnt39117tmu5gjebkku4</sessid>
<session_name>Name Session</session_name>
<user>
<uid>2</uid>
<vID>1</vID>
<roleId>1</roleId>
<username>mail@example.com</username>
<password>password</password>
<dateCreation>2013-05-14 00:00:00</dateCreation>
<dateLastLogin>2013-09-06 09:22:10</dateLastLogin>
<enabled>1</enabled>
<multisession>1</multisession>
<iddID>4</iddID>
<iddName>Nome</iddName>
<iddSurname>Cognome</iddSurname>
<iddMobile>32222222</iddMobile>
<iddEmail>mail@mail.mi</iddEmail>
<iddTelephone></iddTelephone>
<iddFax></iddFax>
<iddNotice></iddNotice>
<roles>manager</roles>
</user>
<result>1</result>
</response>
以下是数据合同:
namespace XmlAnalyzer.Ws {
[Serializable()]
public class LoginResponseContract {
[System.Xml.Serialization.XmlElement("uid")]
public string Uid { get; set; }
[System.Xml.Serialization.XmlElement("username")]
public string Username { get; set; }
[System.Xml.Serialization.XmlElement("iddName")]
public string Name { get; set; }
[System.Xml.Serialization.XmlElement("iddSurname")]
public string Surname { get; set; }
}
}
以下是代码:
HttpWebResponse response = (HttpWebResponse)_webRequest.GetResponse();
Stream responseStream = response.GetResponseStream();
XmlSerializer xs = new XmlSerializer(typeof(LoginResponseContract));
LoginResponseContract loginResponseContract = (LoginResponseContract)xs.Deserialize(streamXmlAuthentication);
这是一个例外:
请求已中止:连接意外关闭。
答案 0 :(得分:1)
这是我尝试对数据进行反序列化。我希望这会对你有所帮助。
class Program {
static void Main(string[] args) {
String xml = "<?xml version=\"1.0\"?>\r\n<response>\r\n<sessid>jsh5ekqnt39117tmu5gjebkku4</sessid>\r\n<session_name>Name Session</session_name>\r\n<user>\r\n<uid>2</uid>\r\n<vID>1</vID>\r\n<roleId>1</roleId>\r\n<username>mail@example.com</username>\r\n<password>password</password>\r\n<dateCreation>2013-05-14 00:00:00</dateCreation>\r\n<dateLastLogin>2013-09-06 09:22:10</dateLastLogin>\r\n<enabled>1</enabled>\r\n<multisession>1</multisession>\r\n<iddID>4</iddID>\r\n<iddName>Nome</iddName>\r\n<iddSurname>Cognome</iddSurname>\r\n<iddMobile>32222222</iddMobile>\r\n<iddEmail>mail@mail.mi</iddEmail>\r\n<iddTelephone></iddTelephone>\r\n<iddFax></iddFax>\r\n<iddNotice></iddNotice>\r\n<roles>manager</roles>\r\n</user>\r\n<result>1</result>\r\n</response>";
Stream s = new MemoryStream(Encoding.Default.GetBytes(xml));
XmlSerializer xs = new XmlSerializer(typeof(Response));
Response loginResponseContract = (Response)xs.Deserialize(s);
Console.WriteLine(loginResponseContract.User.iddName);
Console.ReadKey();
}
}
[Serializable, XmlRoot("response")]
public class Response {
[XmlElement("sessid")]
public string SessionID { get; set; }
[XmlElement("session_name")]
public string SessionName { get; set; }
[XmlElement("user")]
public UserDetail User { get; set; }
[XmlElement("response")]
public int result { get; set; }
}
[Serializable]
public class UserDetail {
[XmlElement("uid")]
public int ID { get; set; }
[XmlElement("vID")]
public int vID { get; set; }
[XmlElement("roleId")]
public int RoleID { get; set; }
[XmlElement("username")]
public string Username { get; set; }
[XmlElement("password")]
public string Password { get; set; }
[XmlElement("dateCreation")]
public string Creation { get; set; }
[XmlElement("dateLastLogin")]
public string LastLogin { get; set; }
[XmlElement("enabled")]
public int Enabled { get; set; }
[XmlElement("multisession")]
public int Multisession { get; set; }
[XmlElement("iddID")]
public string iddID { get; set; }
[XmlElement("iddName")]
public string iddName { get; set; }
[XmlElement("iddSurname")]
public string iddSurname { get; set; }
[XmlElement("iddMobile")]
public string iddMobile { get; set; }
[XmlElement("iddEmail")]
public string iddEmail { get; set; }
[XmlElement("iddTelephone")]
public string iddTelephone { get; set; }
[XmlElement("iddFax")]
public string iddFax { get; set; }
[XmlElement("iddNotice")]
public string iddNotice { get; set; }
[XmlElement("roles")]
public string Roles { get; set; }
}