开发者

how to convert a Linq List into a dataset?

I have a Linq List in which have data more than one tables and these tables has related to each other. in this list it has some another table list property. Like table1 and table2 has related to each other and we have a data in list of table1 and in this list it has automatically table2 data.

Now i want to convert this List into a XML but it throws an error i.e. circular reference error, So now i want to convert this list into a dataset and by using this dataset i can generate a xml.

So can anyone provide us code to generate multiple tables dataset from a List....

or

convert list to xml code....

or

any oth开发者_开发问答er helpful comments............


You really haven't given a lot of information to work with, but I'll take a stab at a basic answer.

Given this class structure:

public class Table1
{
    public string Value;
    public Table2 Table2;
}

public class Table2
{
    public string Value;
}

I can create this list:

var lsttable1 = new List<Table1>()
{
    new Table1()
    {
        Value = "Foo1",
        Table2 = new Table2()
        {
            Value = "Bar1",
        },
    },
    new Table1()
    {
        Value = "Foo2",
        Table2 = new Table2()
        {
            Value = "Bar2",
        },
    },
};

Now, I might want to convert this into XML that looks like this:

<Table1s>
  <Table1 Value="Foo1">
    <Table2 Value="Bar1" />
  </Table1>
  <Table1 Value="Foo2">
    <Table2 Value="Bar2" />
  </Table1>
</Table1s> 

Here's the LINQ code that does it:

var xd =
    new XDocument(
        new XElement(
            "Table1s", 
            from t1 in lsttable1
            select new XElement(
                "Table1",
                new XAttribute("Value", t1.Value),
                new XElement(
                    "Table2",
                    new XAttribute("Value", t1.Table2.Value)
                )
            )
        )
    );

Is that the kind of thing that you wanted?

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜