Groups | Blog | Home
all groups > c# > january 2004 >

c# : XmlSerialzier problem


Dirk Reske
1/3/2004 9:55:45 PM
Hello,

I have following structs

public struct User
{
public string Username;
public string Password;
public string[] Permissions;
}

public struct Users
{
public User[] RemoteUsers;
}

when I Serialize a Users struct I get following File

<?xml version="1.0" encoding="utf-8" ?>
<Users xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<RemoteUsers>
<User>
<Username>Admin</Username>
<Password>21232F297A57A5A743894A0E4A801FC3</Password>
<Permissions>
<string>All</string>
</Permissions>
</User>
</RemoteUsers>
</Users>

all is OK I think..

but when I deserialize it using Users users =
(Users)serializer.Deserialize(streamReader));
I get following error:

Error in XML-document (2,2).
at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader
xmlReader, String encodingStyle)
at System.Xml.Serialization.XmlSerializer.Deserialize(TextReader
textReader)
at Remote.Users.UserControl.LoadUsers()

José Joye
1/3/2004 10:06:55 PM
Don't you need to put the [Serializable] attribute in front of (at least)
Users?

José

"Dirk Reske" <_Freak.2k@gmx.net> a écrit dans le message de
news:eVDMqvj0DHA.2752@TK2MSFTNGP09.phx.gbl...
[quoted text, click to view]

Dirk Reske
1/3/2004 10:26:56 PM
No it doesn't work.

But, when I serialize the structure, all works fine.
I get this error only at deserializing....

"José Joye" <jose.joye@__No_SPam__bluewin__maPS_oN__.ch> schrieb im
Newsbeitrag news:OcaRJ2j0DHA.1660@TK2MSFTNGP09.phx.gbl...
[quoted text, click to view]

José Joye
1/3/2004 11:09:44 PM
BTW, is there any reason why you use XML Serialization instead of SOAP
serialization (using the SOAP formatter)?
I gave a try with the soap one and all is fine


José

"Dirk Reske" <_Freak.2k@gmx.net> a écrit dans le message de
news:O10IFBk0DHA.1708@TK2MSFTNGP12.phx.gbl...
[quoted text, click to view]

Dirk Reske
1/3/2004 11:36:34 PM
I don't know how soap works....I never used it...
can you post some example?

"José Joye" <jose.joye@__No_SPam__bluewin__maPS_oN__.ch> schrieb im
Newsbeitrag news:OQLQQZk0DHA.716@TK2MSFTNGP12.phx.gbl...
[quoted text, click to view]

José Joye
1/3/2004 11:56:31 PM
Here is a small sample that relates to your code:

José

using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Soap;

namespace ConsoleApplication7
{
[Serializable]
public struct User
{
public string Username;
public string Password;
public string[] Permissions;
}

[Serializable]
public struct Users
{
public User[] RemoteUsers;
}
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
FileStream myStream = File.Create("test.xml");
SoapFormatter myXMLFormat = new SoapFormatter();

User u1;
u1.Password = "abc";
u1.Username = "ABC";
string[] perms = new string[2];
perms[0] = "all";
u1.Permissions = perms;

Users users;
users.RemoteUsers = new User[1];
users.RemoteUsers[0] = u1;

myXMLFormat.Serialize(myStream, users);
myStream.Close();

myStream = File.OpenRead("test.xml");
Users userRead = (Users)myXMLFormat.Deserialize(myStream);

Console.ReadLine();

}
}
}

"Dirk Reske" <_Freak.2k@gmx.net> a écrit dans le message de
news:O0OV$nk0DHA.2308@TK2MSFTNGP11.phx.gbl...
[quoted text, click to view]

Frans Bouma
1/4/2004 6:08:06 AM
"Dirk Reske" <_Freak.2k@gmx.net> wrote in news:eVDMqvj0DHA.2752
@TK2MSFTNGP09.phx.gbl:

[quoted text, click to view]

When you remove the header line (<?xml .... ?> ) and then try to
deserialize again, does that work?

FB

--
Get LLBLGen Pro, the new O/R mapper for .NET: http://www.llblgen.com
Dirk Reske
1/4/2004 6:21:11 PM
Hello,

thanks to all, but I find the mistake myself.
I serialize a "Users" struct but tried to deserialize a "User" struct


"Frans Bouma" <perseus.usenetNOSPAM@xs4all.nl> schrieb im Newsbeitrag
news:Xns946699A2F800perseusnewsxs4allnl@207.46.248.16...
[quoted text, click to view]

AddThis Social Bookmark Button