score:8
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.
Source: stackoverflow.com
Related Query
- How to serialize to xml using Linq
- How to flatten a multi level XML into a single level XML using c# code LINQ
- How to load xml code block into existing xml file at a specific node using linq
- How to get elements by name in XML using LINQ
- How to properly search xml document using LINQ C#
- how to add/insert conditional node into XML using linq to XML
- How to reuse a linq expression for 'Where' when using multiple source tables
- how to compare 2 XML using LINQ in C#
- How to read XML file using System.IO.Stream with LINQ
- How to check if XML contains element when using LINQ to XML?
- How do I read/write an Encrypted XML file using LINQ to XML?
- How do I clone a xml element using Linq to Xml
- How can I write the following code more elegantly using LINQ query syntax?
- How can I code an outer join using LINQ and EF6?
- How to add namespace to xml using linq xml
- How to get the value of an XML element using Linq even when empty
- How to Insert a New Node Using LINQ to XML when only xml data is available?
- How do I find an XML element by attribute using LINQ to XML?
- How to get strongly-typed collection from XML using Linq
- how to convert Database Hierarchical Data to XML using ASP.net 3.5 and LINQ
- How to parse XML header and nested sections using LINQ
- How do I add a new element to the existing XML file on Windows Phone 7 using LINQ to XML?
- How Update a node attribute in xml using Linq query?
- How can I perform OrderBy using LINQ on XML data?
- How to Retrieve XML using Linq Lambda?
- How to implement generic method approach using Linq and XML
- How to parse a XML file using Linq - descendants
- How to write this code using the Linq Extension Method-Syntax?
- How can I check the number of calls to the database in LINQ query when using .NET Core and Code First?
- How to read XML using Linq
More Query from same tag
- How to generate an index dictionary from a list of items in C# using linq and lambda
- Is there any pure sql way to delete data from log table in following ways
- How to convert a String[] to an IDictionary<String, String>?
- Could Not Execute Entity Framework Method
- Use ResourceReader to create a HybridDictionary of resources
- How to chain OR clauses, with LINQ?
- LinqPad Entity Framework and the NullExceptionReference
- How to assign value to class instance members using the instance name in a select method
- Accessing one item in a group
- Complex Json type Querying
- Best way to read csv file in C# to improve time efficiency
- Chain together multiple complex WHERE clauses in LINQ to SQL
- Check if values of Dictionary contains an element with certain field value
- Linq to XML Read XPath (Read all nodes)
- LINQ - Querying a list filtered via a Many-to-Many reltionship
- Issue with getting a special attribute in XPath
- Linq to Xml and custom xml entities
- How to use Distinct in entity/linq based on certain columns (Remove duplicated values)
- How do I call a method from within a linq query that is retrieving from an Entity Framework model
- Using LINQ .OrderByDescending not working
- LINQ NHibernate, latest related entity
- Selecting changes in values with Lambda, Linq, Entity Framework
- StackOverflowException on nested query, small item count
- Nested classes query and join
- LINQ in ItemBinding to add columns to DataGrid
- Linq to entities ignore casing, NotSupportedException
- Equality between two enumerables
- how to write linq query with where clause to get records between 9 am to 5 pm
- Multi-Table select
- distinct value from a column in a grid using linq