XmlSerialzier problem

D

Dirk Reske

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()
 
J

José Joye

Don't you need to put the [Serializable] attribute in front of (at least)
Users?

José
 
D

Dirk Reske

No it doesn't work.

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

José Joye said:
Don't you need to put the [Serializable] attribute in front of (at least)
Users?

José

Dirk Reske said:
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()
 
J

José Joye

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 said:
No it doesn't work.

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

José Joye said:
Don't you need to put the [Serializable] attribute in front of (at least)
Users?

José

Dirk Reske said:
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()
 
D

Dirk Reske

I don't know how soap works....I never used it...
can you post some example?

José Joye said:
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 said:
No it doesn't work.

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

José Joye said:
Don't you need to put the [Serializable] attribute in front of (at least)
Users?

José

"Dirk Reske" <[email protected]> a écrit dans le message de
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()
 
J

José Joye

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 said:
I don't know how soap works....I never used it...
can you post some example?

José Joye said:
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 said:
No it doesn't work.

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

Newsbeitrag Don't you need to put the [Serializable] attribute in front of (at least)
Users?

José

"Dirk Reske" <[email protected]> a écrit dans le message de
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()
 
F

Frans Bouma

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()

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

FB
 
D

Dirk Reske

Hello,

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


Frans Bouma said:
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()

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

FB
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top