[英]How can I retrieve XML nodes dynamically from SQL Server and use XDocument to build a file from a template with them?
I have an XML template with all the headers I need. 我有一个XML模板,其中包含我需要的所有标头。 I load it with
XDocument
, and then build the nodes using XElements
. 我使用
XDocument
加载它,然后使用XElements
构建节点。 However, this means I am currently hard coding the data bits I need to go into my file. 但是,这意味着我目前正在对进入文件所需的数据位进行硬编码。
I noticed with SQL Server I can use the XML
datatype to store data in the database. 我注意到使用SQL Server可以使用
XML
数据类型将数据存储在数据库中。 Using this approach it would be better to look through each record and dynamically build the XML
file. 使用这种方法,最好浏览每条记录并动态构建
XML
文件。
However, I see some issues with my approach. 但是,我发现我的方法存在一些问题。 There may be two nodes that share the same parent.
可能有两个节点共享同一父节点。 Is there a way to store the nodes and their parent, and use something in
XDocument
to merge them? 有没有办法存储节点及其父节点,并在
XDocument
使用某些东西来合并它们?
Here is a sample chunk of my XML 这是我的XML的样本块
<AddInfoCollection>
<AddInfo>
<Key>TransportReference</Key>
<Value>666777888</Value>
</AddInfo>
<AddInfo>
<Key>UI_NKCarrierSCAC</Key>
<Value>ABCD</Value>
</AddInfo>
<AddInfo>
<Key>SchDLoading</Key>
<Value>1234</Value>
</AddInfo>
<AddInfo>
<Key>SchDArrival</Key>
<Value>12345</Value>
</AddInfo>
</AddInfoCollection>
In the database, I'd imagine the table would be: 在数据库中,我想表应该是:
Field (varchar(200)
Value (varchar(200)
XML
Sample: 样品:
Field Value XML
----------------------------------------------------------------
TransportReference 666777888 <AddInfoCollection>
<AddInfo>
<Key>TransportReference</Key>
<Value>666777888</Value>
</AddInfo>
<AddInfoCollection>
Use SELECT... FOR XML . 使用SELECT ... FOR XML 。
For example: 例如:
USE ADVENTUREWORKS
SELECT TOP 2 * FROM HUMANRESOURCES.EMPLOYEE FOR XML PATH
This returns the resultset as the following XML. 这将返回结果集为以下XML。 Use other variants of SELECT...FOR XML to tailor the XML to your requirements.
使用SELECT ... FOR XML的其他变体来定制XML以符合您的要求。
<row>
<EmployeeID>1</EmployeeID>
<NationalIDNumber>14417807</NationalIDNumber>
<ContactID>1209</ContactID>
<LoginID>adventure-works\guy1</LoginID>
<ManagerID>16</ManagerID>
<Title>Production Technician - WC60</Title>
<BirthDate>1972-05-15T00:00:00</BirthDate>
<MaritalStatus>M</MaritalStatus>
<Gender>M</Gender>
<HireDate>1996-07-31T00:00:00</HireDate>
<SalariedFlag>0</SalariedFlag>
<VacationHours>21</VacationHours>
<SickLeaveHours>30</SickLeaveHours>
<CurrentFlag>1</CurrentFlag>
<rowguid>AAE1D04A-C237-4974-B4D5-935247737718</rowguid>
<ModifiedDate>2004-07-31T00:00:00</ModifiedDate>
</row>
<row>
<EmployeeID>2</EmployeeID>
<NationalIDNumber>253022876</NationalIDNumber>
<ContactID>1030</ContactID>
<LoginID>adventure-works\kevin0</LoginID>
<ManagerID>6</ManagerID>
<Title>Marketing Assistant</Title>
<BirthDate>1977-06-03T00:00:00</BirthDate>
<MaritalStatus>S</MaritalStatus>
<Gender>M</Gender>
<HireDate>1997-02-26T00:00:00</HireDate>
<SalariedFlag>0</SalariedFlag>
<VacationHours>42</VacationHours>
<SickLeaveHours>41</SickLeaveHours>
<CurrentFlag>1</CurrentFlag>
<rowguid>1B480240-95C0-410F-A717-EB29943C8886</rowguid>
<ModifiedDate>2004-07-31T00:00:00</ModifiedDate>
</row>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.