Reputation: 24318
I presume this is possible? Basically i am receiving using LINQ (LINQ2Entities Entity Framework) data and return it to an IQUERYABLE once i this data i need to transform it to XML in a Memory Stream and a physical file on the hard disk - streamwriter??
Does anyone know if this is possible?
Any help really appreciated
Do i need to use LINQtoXML to accomplish this?
Any examples or tutorials anybody knows of would be great.
Thanks again
EDIT
After a little bit of investigation i think i require a XML Serializer for LINQ2Entities / IQueryable??
Upvotes: 1
Views: 4316
Reputation: 374
I know this question is a old, but I wanted to show my solution to this problem.
public static string CreateXml<T>(IQueryable<T> thisQueryable)
{
var thisList = thisQueryable.ToList();
var xmlResult = "";
using (var stringWriter = new StringWriter())
{
using (var xmlWriter = new XmlTextWriter(stringWriter))
{
var serializer = new XmlSerializer(typeof(List<T>));
serializer.Serialize(xmlWriter, thisList);
}
xmlResult = stringWriter.ToString();
}
return xmlResult;
}
Basically this just takes your IQueryable<T>
and serializes it to XML
and returns that XML
as a string
.
Then you'd basically take that string and....
var xmlDoc = new XmlDocument();
xmlDoc.Load(xmlResult);
Hope this helps for any future visitors.
Upvotes: 1
Reputation: 21
Entity classes are by default Data Contracts, which means that they can be serialized based on the fields they contain:
http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datacontractserializer.aspx
Upvotes: 2
Reputation: 263047
LINQ to XML is probably the best choice. You can use functional construction to create an XML tree in a single statement, like:
using (ContactEntities context = new ContactEntities()) {
XDocument xml = new XDocument(
new XElement("contacts",
from contact in context.Contacts
orderby contact.ContactId
select new XElement("contact",
new XAttribute("contactId", contact.ContactId),
new XElement("firstName", contact.FirstName),
new XElement("lastName", contact.LastName))));
xml.Save(yourStream);
}
Upvotes: 2