开发者

Convert a LINQ List into XML in Asp.net MVC architecture

开发者 https://www.devze.com 2023-04-03 05:51 出处:网络
I have a list in LINQtoSQL and it has various relation in tables and now i want to convert this list into XML.

I have a list in LINQtoSQL and it has various relation in tables and now i want to convert this list into XML.

I am using this code

new XmlSerializer(typeof(T), IncludedTypes).Serialize(xmlWriter, this.Data);

where this.Data has the list data.

but it throws an error

There was an e开发者_开发知识库rror generating the XML document.

because our tables has related to each other and in our list it also maintains all tables relations

Now How to convert a list into a xml?


circular reference was detected while serializing an object of type abc.Core.Model.Channel that means Channel object has a property refers to another object and that object has a property refers to Channel object.

for example

Class Parent
{
   public Child Child
   {
       get;
       set;
   }
}
Class Child
{
   public Parent Parent
   {
       get;
       set;
   }
}

When serializing Parent.Child it will serialize Child.Parent so it will serialize Parent.Child again etc.

to solve this problem check this post


Gaurav,

Based on the comments in your question re the inner exception:

A circular reference was detected while serializing an object of 
type abc.Core.Model.Channel

I would suggest that you decorate ANY parent objects with a [ScriptIgnore] or [XmlIgnore] attribute. This will prevent the circular references from being actioned as the xml gets saved. The issue is due to the fact that the parent object in turn refers again to the child object. this reference goes on to infinity (and beyond :)) and will never resolve without you indicating that you don not wish to serialize parent objects.

0

精彩评论

暂无评论...
验证码 换一张
取 消