如何将Linq查询结果转换为XML?

如何将Linq查询结果转换为XML?

问题描述:

Linq to XML领域的新手...

Newbie in the Linq to XML arena...

我有一个带有结果的Linq查询,我想将这些结果转换为XML.我猜肯定有一种相对简单的方法可以做到,但是我找不到它...

I have a Linq query with results, and I'd like to transform those results into XML. I'm guessing there must be a relatively easy way to do it, but I can't find it...

谢谢!

一个示例.你应该明白这个主意.

An Example. You should get the idea.

XElement xml = new XElement("companies",
            from company in db.CustomerCompanies
            orderby company.CompanyName
            select new XElement("company",
                new XAttribute("CompanyId", company.CompanyId),
                new XElement("CompanyName", company.CompanyName),
                new XElement("SapNumber", company.SapNumber),
                new XElement("RootCompanyId", company.RootCompanyId),
                new XElement("ParentCompanyId", company.ParentCompanyId)
                )
            );