【发布时间】:2017-06-04 23:07:52
【问题描述】:
我有一个 xml 文档,我正在使用 T-SQL 提取信息并保存在表中。但问题是我提取了多个 xml 并有多个数据集保存在数据库中。我想知道是否可以使用 Linq 从我的 C# 代码上的这些多个 xml 文件中提取数据并创建一个列表并使用批量插入,而不必每次都将 xml 发送到存储过程。 我的 T-Sql 代码提取信息:
select x.i.value('ReportCell[1]/Value[1]', 'varchar(250)') as AccountName, x.i.value('ReportCell[1]/Attributes[1]/ReportCellAttribute[1]/Value[1]', 'varchar(250)') as AccountId, x.i.value('ReportCell[2]/Value[1]', 'varchar(250)') as Amnount
from @xml.nodes('//Cells') as x(i)
where x.i.value('../RowType[1]', 'varchar(250)') = 'Row' and x.i.value('ReportCell[1]/Attributes[1]', 'varchar(250)') is not null
Xml 文件是:
<?xml version="1.0" encoding="utf-16"?>
<Report xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<ReportID>BalanceSheet</ReportID>
<ReportName>Balance Sheet</ReportName>
<ReportType>BalanceSheet</ReportType>
<ReportTitles>
<string>Balance Sheet</string>
<string>Ulysses It 6</string>
<string>As at 31 October 2016</string>
</ReportTitles>
<ReportDate>18 January 2017</ReportDate>
<UpdatedDateUTC>2017-01-18T01:07:41.654Z</UpdatedDateUTC>
<Fields />
<Rows>
<ReportRow>
<RowType>Header</RowType>
<Cells>
<ReportCell>
<Value />
</ReportCell>
<ReportCell>
<Value>31 Oct 2016</Value>
</ReportCell>
<ReportCell>
<Value>31 Oct 2015</Value>
</ReportCell>
</Cells>
</ReportRow>
<ReportRow>
<RowType>Section</RowType>
<Title>Assets</Title>
<Rows />
</ReportRow>
<ReportRow>
<RowType>Section</RowType>
<Title>Bank</Title>
<Rows>
<ReportRow>
<RowType>Row</RowType>
<Cells>
<ReportCell>
<Value>Ulysses Six</Value>
<Attributes>
<ReportCellAttribute>
<Value>9e44f52a-90f4-4e9f-88f5-2dd9a33fc1c6</Value>
<Id>account</Id>
</ReportCellAttribute>
</Attributes>
</ReportCell>
<ReportCell>
<Value>486000.00</Value>
<Attributes>
<ReportCellAttribute>
<Value>9e44f52a-90f4-4e9f-88f5-2dd9a33fc1c6</Value>
<Id>account</Id>
</ReportCellAttribute>
</Attributes>
</ReportCell>
<ReportCell>
<Value>0.00</Value>
<Attributes>
<ReportCellAttribute>
<Value>9e44f52a-90f4-4e9f-88f5-2dd9a33fc1c6</Value>
<Id>account</Id>
</ReportCellAttribute>
</Attributes>
</ReportCell>
</Cells>
</ReportRow>
<ReportRow>
<RowType>SummaryRow</RowType>
<Cells>
<ReportCell>
<Value>Total Bank</Value>
</ReportCell>
<ReportCell>
<Value>486000.00</Value>
</ReportCell>
<ReportCell>
<Value>0.00</Value>
</ReportCell>
</Cells>
</ReportRow>
</Rows>
</ReportRow>
<ReportRow>
<RowType>Section</RowType>
<Title>Current Assets</Title>
<Rows>
<ReportRow>
<RowType>Row</RowType>
<Cells>
<ReportCell>
<Value>Accounts Receivable</Value>
<Attributes>
<ReportCellAttribute>
<Value>25e89097-8895-445c-8315-7efe50dc3be7</Value>
<Id>account</Id>
</ReportCellAttribute>
</Attributes>
</ReportCell>
<ReportCell>
<Value>375000.00</Value>
<Attributes>
<ReportCellAttribute>
<Value>25e89097-8895-445c-8315-7efe50dc3be7</Value>
<Id>account</Id>
</ReportCellAttribute>
</Attributes>
</ReportCell>
<ReportCell>
<Value>0.00</Value>
<Attributes>
<ReportCellAttribute>
<Value>25e89097-8895-445c-8315-7efe50dc3be7</Value>
<Id>account</Id>
</ReportCellAttribute>
</Attributes>
</ReportCell>
</Cells>
</ReportRow>
<ReportRow>
<RowType>SummaryRow</RowType>
<Cells>
<ReportCell>
<Value>Total Current Assets</Value>
</ReportCell>
<ReportCell>
<Value>375000.00</Value>
</ReportCell>
<ReportCell>
<Value>0.00</Value>
</ReportCell>
</Cells>
</ReportRow>
</Rows>
</ReportRow>
<ReportRow>
<RowType>Section</RowType>
<Title />
<Rows>
<ReportRow>
<RowType>SummaryRow</RowType>
<Cells>
<ReportCell>
<Value>Total Assets</Value>
</ReportCell>
<ReportCell>
<Value>861000.00</Value>
</ReportCell>
<ReportCell>
<Value>0.00</Value>
</ReportCell>
</Cells>
</ReportRow>
</Rows>
</ReportRow>
<ReportRow>
<RowType>Section</RowType>
<Title>Liabilities</Title>
<Rows />
</ReportRow>
<ReportRow>
<RowType>Section</RowType>
<Title>Current Liabilities</Title>
<Rows>
<ReportRow>
<RowType>Row</RowType>
<Cells>
<ReportCell>
<Value>GST</Value>
<Attributes>
<ReportCellAttribute>
<Value>81ee7772-593d-48d3-851d-0bd68149d527</Value>
<Id>account</Id>
</ReportCellAttribute>
</Attributes>
</ReportCell>
<ReportCell>
<Value>78273.51</Value>
<Attributes>
<ReportCellAttribute>
<Value>81ee7772-593d-48d3-851d-0bd68149d527</Value>
<Id>account</Id>
</ReportCellAttribute>
</Attributes>
</ReportCell>
<ReportCell>
<Value>0.00</Value>
<Attributes>
<ReportCellAttribute>
<Value>81ee7772-593d-48d3-851d-0bd68149d527</Value>
<Id>account</Id>
</ReportCellAttribute>
</Attributes>
</ReportCell>
</Cells>
</ReportRow>
<ReportRow>
<RowType>SummaryRow</RowType>
<Cells>
<ReportCell>
<Value>Total Current Liabilities</Value>
</ReportCell>
<ReportCell>
<Value>78273.51</Value>
</ReportCell>
<ReportCell>
<Value>0.00</Value>
</ReportCell>
</Cells>
</ReportRow>
</Rows>
</ReportRow>
<ReportRow>
<RowType>Section</RowType>
<Title />
<Rows>
<ReportRow>
<RowType>SummaryRow</RowType>
<Cells>
<ReportCell>
<Value>Total Liabilities</Value>
</ReportCell>
<ReportCell>
<Value>78273.51</Value>
</ReportCell>
<ReportCell>
<Value>0.00</Value>
</ReportCell>
</Cells>
</ReportRow>
</Rows>
</ReportRow>
<ReportRow>
<RowType>Section</RowType>
<Title />
<Rows>
<ReportRow>
<RowType>Row</RowType>
<Cells>
<ReportCell>
<Value>Net Assets</Value>
</ReportCell>
<ReportCell>
<Value>782726.49</Value>
</ReportCell>
<ReportCell>
<Value>0.00</Value>
</ReportCell>
</Cells>
</ReportRow>
</Rows>
</ReportRow>
<ReportRow>
<RowType>Section</RowType>
<Title>Equity</Title>
<Rows>
<ReportRow>
<RowType>Row</RowType>
<Cells>
<ReportCell>
<Value>Current Year Earnings</Value>
<Attributes>
<ReportCellAttribute>
<Value>abababab-abab-abab-abab-abababababab</Value>
<Id>account</Id>
</ReportCellAttribute>
</Attributes>
</ReportCell>
<ReportCell>
<Value>324545.13</Value>
<Attributes>
<ReportCellAttribute>
<Value>abababab-abab-abab-abab-abababababab</Value>
<Id>account</Id>
</ReportCellAttribute>
<ReportCellAttribute>
<Value>7/1/2016</Value>
<Id>fromDate</Id>
</ReportCellAttribute>
<ReportCellAttribute>
<Value>10/31/2016</Value>
<Id>toDate</Id>
</ReportCellAttribute>
</Attributes>
</ReportCell>
<ReportCell>
<Value>0.00</Value>
<Attributes>
<ReportCellAttribute>
<Value>abababab-abab-abab-abab-abababababab</Value>
<Id>account</Id>
</ReportCellAttribute>
<ReportCellAttribute>
<Value>7/1/2015</Value>
<Id>fromDate</Id>
</ReportCellAttribute>
<ReportCellAttribute>
<Value>10/31/2015</Value>
<Id>toDate</Id>
</ReportCellAttribute>
</Attributes>
</ReportCell>
</Cells>
</ReportRow>
<ReportRow>
<RowType>Row</RowType>
<Cells>
<ReportCell>
<Value>Retained Earnings</Value>
<Attributes>
<ReportCellAttribute>
<Value>86d10e93-151d-4b89-bb65-6df9bbacd2e3</Value>
<Id>account</Id>
</ReportCellAttribute>
</Attributes>
</ReportCell>
<ReportCell>
<Value>458181.36</Value>
<Attributes>
<ReportCellAttribute>
<Value>86d10e93-151d-4b89-bb65-6df9bbacd2e3</Value>
<Id>account</Id>
</ReportCellAttribute>
<ReportCellAttribute>
<Value />
<Id>fromDate</Id>
</ReportCellAttribute>
<ReportCellAttribute>
<Value>10/31/2016</Value>
<Id>toDate</Id>
</ReportCellAttribute>
</Attributes>
</ReportCell>
<ReportCell>
<Value>0.00</Value>
<Attributes>
<ReportCellAttribute>
<Value>86d10e93-151d-4b89-bb65-6df9bbacd2e3</Value>
<Id>account</Id>
</ReportCellAttribute>
<ReportCellAttribute>
<Value />
<Id>fromDate</Id>
</ReportCellAttribute>
<ReportCellAttribute>
<Value>10/31/2015</Value>
<Id>toDate</Id>
</ReportCellAttribute>
</Attributes>
</ReportCell>
</Cells>
</ReportRow>
<ReportRow>
<RowType>SummaryRow</RowType>
<Cells>
<ReportCell>
<Value>Total Equity</Value>
</ReportCell>
<ReportCell>
<Value>782726.49</Value>
</ReportCell>
<ReportCell>
<Value>0.00</Value>
</ReportCell>
</Cells>
</ReportRow>
</Rows>
</ReportRow>
</Rows>
</Report>
【问题讨论】:
-
使用 XmlSerializer(然后使用 Deserialize 方法)或使用 XMLTextReader
标签: c# xml database xml-parsing linq-to-xml