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


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",,
    new xelement("address", coll.address),
    new xelement("persons", =>
      new xelement("person",
        new xelement("gender", p.gender),
        new xelement("city",


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


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

// this is the test class we want to 
// serialize:
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
    private int willbeignored1 = 1;
    private int willbeignored2 = 1;


// example code

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

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

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


#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


the test.xml file will look like this:

<?xml version="1.0"?>
<testclass xmlns:xsi="" xmlns:xsd="">

#region load the object

// create a new file stream for reading the xml file
filestream readfilestream = new filestream(@"c:\test.xml",,,;

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

// cleanup


// test the new loaded object:;

foreach (string setting in loadedobj.settings);


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);, 0);

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


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",,
               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