Linq - 查询表并以 XML 形式返回结果
我有以下表结构:
CREATE TABLE [Report].[MesReport](
[MesReportID] [int] IDENTITY(1,1) NOT NULL,
[ParentID] [int] NOT NULL,
[ReportTitle] [nvarchar](80) NOT NULL,
[ReportName] [nvarchar](80) NOT NULL,
[DatabaseServer] [nvarchar](80) NOT NULL,
[DatabaseName] [nvarchar](50) NOT NULL,
[Login] [nvarchar](80) NOT NULL,
[ReportFile] [varbinary](max) NULL
我想创建一个 Linq 查询来查询该表并生成以下 XML。有人可以帮忙吗?
<MesReports>
<MesReport>
<ReportTitle>Mes to Sap Reconciliation Reports</ReportTitle>
<ReportName>None</ReportName>
<DatabaseServer>None</DatabaseServer>
<Database>None</Database>
<Login>None</Login>
<MesReportId>1</MesReportId>
<ParentId>-1</ParentId>
</MesReport>
<MesReport>
<ReportTitle>Finished Goods</ReportTitle>
<ReportName>None</ReportName>
<DatabaseServer>None</DatabaseServer>
<Database>None</Database>
<Login>None</Login>
<MesReportId>2</MesReportId>
<ParentId>1</ParentId>
</MesReport>
<MesReport>
<ReportTitle>Semi-Finished Goods</ReportTitle>
<ReportName>None</ReportName>
<DatabaseServer>None</DatabaseServer>
<Database>None</Database>
<Login>None</Login>
<MesReportId>3</MesReportId>
<ParentId>1</ParentId>
</MesReport>
<MesReport>
<ReportTitle>Warranty Reports</ReportTitle>
<ReportName>None</ReportName>
<DatabaseServer>None</DatabaseServer>
<Database>None</Database>
<Login>None</Login>
<MesReportId>4</MesReportId>
<ParentId>-1</ParentId>
</MesReport>
<MesReport>
<ReportTitle>Box - PBG</ReportTitle>
<ReportName>Warranty Box - PBG</ReportName>
<DatabaseServer>MyServer</DatabaseServer>
<Database>MESProduction</Database>
<Login>6QDI+IoQbkZKJpVBcRJcNtoqR62606Q2</Login>
<MesReportId>5</MesReportId>
<ParentId>4</ParentId>
</MesReport>
<MesReport>
<ReportTitle>Box - FFO</ReportTitle>
<ReportName>Warranty Box - FFO</ReportName>
<DatabaseServer>MyServer</DatabaseServer>
<Database>MESProduction</Database>
<Login>6QDI+IoQbkZKJpVBcRJcNtoqR62606Q2</Login>
<MesReportId>6</MesReportId>
<ParentId>4</ParentId>
</MesReport>
<MesReport>
<ReportTitle>Bin Count - PBG</ReportTitle>
<ReportName>Bin Count - PBG</ReportName>
<DatabaseServer>MyServer</DatabaseServer>
<Database>MESProduction</Database>
<Login>6QDI+IoQbkZKJpVBcRJcNtoqR62606Q2</Login>
<MesReportId>7</MesReportId>
<ParentId>4</ParentId>
</MesReport>
<MesReport>
<ReportTitle>Bin Count - FFO</ReportTitle>
<ReportName>Bin Count - FFO</ReportName>
<DatabaseServer>MyServer</DatabaseServer>
<Database>MESProduction</Database>
<Login>6QDI+IoQbkZKJpVBcRJcNtoqR62606Q2</Login>
<MesReportId>8</MesReportId>
<ParentId>4</ParentId>
</MesReport>
</MesReports>
I have the following table structure:
CREATE TABLE [Report].[MesReport](
[MesReportID] [int] IDENTITY(1,1) NOT NULL,
[ParentID] [int] NOT NULL,
[ReportTitle] [nvarchar](80) NOT NULL,
[ReportName] [nvarchar](80) NOT NULL,
[DatabaseServer] [nvarchar](80) NOT NULL,
[DatabaseName] [nvarchar](50) NOT NULL,
[Login] [nvarchar](80) NOT NULL,
[ReportFile] [varbinary](max) NULL
I want to create a Linq query that will query this table and generate the following XML. Can someone help with this?
<MesReports>
<MesReport>
<ReportTitle>Mes to Sap Reconciliation Reports</ReportTitle>
<ReportName>None</ReportName>
<DatabaseServer>None</DatabaseServer>
<Database>None</Database>
<Login>None</Login>
<MesReportId>1</MesReportId>
<ParentId>-1</ParentId>
</MesReport>
<MesReport>
<ReportTitle>Finished Goods</ReportTitle>
<ReportName>None</ReportName>
<DatabaseServer>None</DatabaseServer>
<Database>None</Database>
<Login>None</Login>
<MesReportId>2</MesReportId>
<ParentId>1</ParentId>
</MesReport>
<MesReport>
<ReportTitle>Semi-Finished Goods</ReportTitle>
<ReportName>None</ReportName>
<DatabaseServer>None</DatabaseServer>
<Database>None</Database>
<Login>None</Login>
<MesReportId>3</MesReportId>
<ParentId>1</ParentId>
</MesReport>
<MesReport>
<ReportTitle>Warranty Reports</ReportTitle>
<ReportName>None</ReportName>
<DatabaseServer>None</DatabaseServer>
<Database>None</Database>
<Login>None</Login>
<MesReportId>4</MesReportId>
<ParentId>-1</ParentId>
</MesReport>
<MesReport>
<ReportTitle>Box - PBG</ReportTitle>
<ReportName>Warranty Box - PBG</ReportName>
<DatabaseServer>MyServer</DatabaseServer>
<Database>MESProduction</Database>
<Login>6QDI+IoQbkZKJpVBcRJcNtoqR62606Q2</Login>
<MesReportId>5</MesReportId>
<ParentId>4</ParentId>
</MesReport>
<MesReport>
<ReportTitle>Box - FFO</ReportTitle>
<ReportName>Warranty Box - FFO</ReportName>
<DatabaseServer>MyServer</DatabaseServer>
<Database>MESProduction</Database>
<Login>6QDI+IoQbkZKJpVBcRJcNtoqR62606Q2</Login>
<MesReportId>6</MesReportId>
<ParentId>4</ParentId>
</MesReport>
<MesReport>
<ReportTitle>Bin Count - PBG</ReportTitle>
<ReportName>Bin Count - PBG</ReportName>
<DatabaseServer>MyServer</DatabaseServer>
<Database>MESProduction</Database>
<Login>6QDI+IoQbkZKJpVBcRJcNtoqR62606Q2</Login>
<MesReportId>7</MesReportId>
<ParentId>4</ParentId>
</MesReport>
<MesReport>
<ReportTitle>Bin Count - FFO</ReportTitle>
<ReportName>Bin Count - FFO</ReportName>
<DatabaseServer>MyServer</DatabaseServer>
<Database>MESProduction</Database>
<Login>6QDI+IoQbkZKJpVBcRJcNtoqR62606Q2</Login>
<MesReportId>8</MesReportId>
<ParentId>4</ParentId>
</MesReport>
</MesReports>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里的技巧是,您需要两个单独的 LINQ 语句,一个 linq-to-sql 来读取数据,一个 linq-to-xml 来写入数据。
The trick here is that you need two separate LINQ statement, one linq-to-sql to read the data, and one linq-to-xml to write it.