score:8

Accepted answer

you can't serialize with linq. you can use xmlserializer.

  xmlserializer serializer = new xmlserializer(typeof(college));

  // create a filestream to write with.
  stream writer = new filestream(filename, filemode.create);
  // serialize the object, and close the textwriter
  serializer.serialize(writer, i);
  writer.close();

score:0

i'm not sure if that is what you want, but to make an xml-document out of this:

college coll = ...
xdocument doc = new xdocument(
  new xelement("college",
    new xelement("name", coll.name),
    new xelement("address", coll.address),
    new xelement("persons", coll.persons.select(p =>
      new xelement("person",
        new xelement("gender", p.gender),
        new xelement("city", p.city)
      )
    )
  )
);

score:1

you have to use the xml serialization

static public void serializetoxml(college college)
{
  xmlserializer serializer = new xmlserializer(typeof(college));
  textwriter textwriter = new streamwriter(@"c:\college.xml");
  serializer.serialize(textwriter, college);
  textwriter.close();
}

score:1

you can't use linq. look at the below code as an example.

// this is the test class we want to 
// serialize:
[serializable()]
public class testclass
{
    private string somestring;
    public string somestring
    {
        get { return somestring; }
        set { somestring = value; }
    }

    private list<string> settings = new list<string>();
    public list<string> settings
    {
        get { return settings; }
        set { settings = value; }
    }

    // these will be ignored
    [nonserialized()]
    private int willbeignored1 = 1;
    private int willbeignored2 = 1;

}

// example code

// this example requires:
// using system.xml.serialization;
// using system.io;

// create a new instance of the test class
testclass testobj = new testclass();

// set some dummy values
testobj.somestring = "foo";

testobj.settings.add("a");
testobj.settings.add("b");
testobj.settings.add("c");


#region save the object

// create a new xmlserializer instance with the type of the test class
xmlserializer serializerobj = new xmlserializer(typeof(testclass));

// create a new file stream to write the serialized object to a file
textwriter writefilestream = new streamwriter(@"c:\test.xml");
serializerobj.serialize(writefilestream, testobj);

// cleanup
writefilestream.close();

#endregion


/*
the test.xml file will look like this:

<?xml version="1.0"?>
<testclass xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:xsd="http://www.w3.org/2001/xmlschema">
  <somestring>foo</somestring>
  <settings>
    <string>a</string>
    <string>b</string>
    <string>c</string>
  </settings>
</testclass>         
*/

#region load the object

// create a new file stream for reading the xml file
filestream readfilestream = new filestream(@"c:\test.xml", filemode.open, fileaccess.read, fileshare.read);

// load the object saved above by using the deserialize function
testclass loadedobj = (testclass)serializerobj.deserialize(readfilestream);

// cleanup
readfilestream.close();

#endregion


// test the new loaded object:
messagebox.show(loadedobj.somestring);

foreach (string setting in loadedobj.settings)
    messagebox.show(setting);

score:1

you can use that if you needed xdocument object after serialization

dataclass dc = new dataclass();

xmlserializer x = new xmlserializer(typeof(dataclass));
memorystream ms = new memorystream();
x.serialize(ms, dc);
ms.seek(0, 0);

xdocument xdocument = xdocument.load(ms); // here it is!

score:7

not sure why people are saying you can't serialize/deserialize with linq. custom serialization is still serialization:

public static college deserialize(xelement collegexml)
{
    return new college()
           {
               name = (string)collegexml.element("name"),
               address = (string)collegexml.element("address"),
               persons = (from personxml in collegexml.element("persons").elements("person")
                          select person.deserialize(personxml)).tolist()
           }
}

public static xelement serialize(college college)
{
    return new xelement("college",
               new xelement("name", college.name),
               new xelement("address", college.address)
               new xelement("persons", (from p in college.persons
                                        select person.serialize(p)).tolist()));
);

note, this probably isn't the greatest approach, but it's answering the question at least.


Related Query

More Query from same tag