views:

85

answers:

2

I am writing a program that will basically make a xml copy of most things in the local computer SAM store for users.

Currently it only prints a XMLElement for each user but it does not print the attributes for them.

<?xml version="1.0" encoding="utf-8"?>
<WindowsUserList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
  <windowsUserEntry />
  <windowsUserEntry />
  ...
  <windowsUserEntry />
  <windowsUserEntry />
</WindowsUserList>

This is my main code, the check box text has formatted name for a search as its text (accounting* for example to get users accounting1, 2, 3 ect..)

windowsUserList listUsers = new windowsUserList();
PrincipalContext context = new PrincipalContext(ContextType.Machine, Settings.Default.ipAddress, Settings.Default.username, Settings.Default.password);
foreach (CheckBox cbx in groupBox1.Controls.OfType<CheckBox>())
{
  if (cbx.Checked)
  {
    UserPrincipal usr = new UserPrincipal(context);
    if (cbx.Text == "")
    {
      usr.Name = txtCustom.Text;
    }
    else
    {
      usr.Name = cbx.Text;
    }
    PrincipalSearcher search = new PrincipalSearcher(usr);
    PrincipalSearchResult<Principal> results = search.FindAll();
    foreach (Principal result in results)
    {
      listUsers.AddUser(new windowsUserEntry((UserPrincipal)result));
    } // foreach (Principal result in results)
  }//if (cbx.Checked)
}//foreach (CheckBox cbx in groupBox1.Controls.OfType<CheckBox>())
XmlSerializer s = new XmlSerializer(typeof(windowsUserList));
TextWriter w = new StreamWriter(dlgSave.OpenFile());
s.Serialize(w, listUsers);
w.Close();

The code for windows user list/entry is very long so I posted it to pastebin

+2  A: 

The properties of your windowsUserEntry class are read-only. XML serialization can only serialize read-write properties

Thomas Levesque
Is there different function than Serialize that can be used on read only attributes?
Scott Chamberlain
Can you use the DataContractSerializer? What version of .NET are you running?
John Saunders
I decided to just flesh out the class and correctly implement the setters, for the read only objects I created a new exception that I throw OjectIsReadOnly in the setter
Scott Chamberlain
+1  A: 

If you include empty setters for the properties of windowsUserEntry, you'll be able to serialize (but not deserialize). Or you could throw an exception (NotImplementedException?) in the setters. I don't think there's any other way to serialze your objects short of implementing your own Xml Serialization.

fre0n