[英]C# Create XML File from with more than one dataset (one being for details)
Im not the most experienced developer so excuse me if I have not used the correct terms here. 我不是最有经验的开发人员,如果我在这里使用的术语不正确,请原谅。
I want to create an XML file in visual studio using C# from data from SQL server. 我想使用SQL Server中的C#在Visual Studio中创建XML文件。
The data will come from two dataset. 数据将来自两个数据集。 The first will be the main 'header' details and then the second will be the 'line detail' for each of these headers.
第一个将是主要的“标题”详细信息,然后第二个将是每个这些标题的“行详细信息”。
I want the code to first loop through the header dataset, add all columns to the xml and then before moving onto the next row, it will loop through the second dataset and find the rows the match up to the current row of the first dataset (by ID) and add all those rows to the XML. 我希望代码首先循环遍历头数据集,将所有列添加到xml,然后再移至下一行,然后遍历第二个数据集并查找与第一个数据集的当前行匹配的行(按ID),然后将所有这些行添加到XML中。
There can only be one unique header but this can have more than one line detail. 只能有一个唯一的标头,但是可以包含多个行细节。 The XML should look something like below, where header is the from the first dataset and line is the second (Just example data):
XML如下所示,其中header是来自第一个数据集的,而line是第二个数据集(仅示例数据):
<?xml version="1.0"?>
<Header>
<ID>1</ID>
<Name>James</Name>
<Address>12 London Road</Address>
<TelNumber>07543430921</TelNumber>
<Line>
<LineItem>
<OrderNo>0001</OrderNo>
<Price>100.00</Price>
<Tax>20.00</Tax>
<Delivery>5.00</Delivery>
</LineItem>
<LineItem>
<OrderNo>0002</OrderNo>
<Price>200.00</Price>
<Tax>20.00</Tax>
<Delivery>6.00</Delivery>
</LineItem>
</Line>
</Header>
<Header>
<ID>2</ID>
<Name>Danny</Name>
<Address>56 High Street</Address>
<TelNumber>07698903111</TelNumber>
<Line>
<LineItem>
<OrderNo>0005</OrderNo>
<Price>300.00</Price>
<Tax>20.00</Tax>
<Delivery>9.99</Delivery>
</LineItem>
</Line>
</Header>
I have no idea how to code this so any help with me greatly appreciated. 我不知道该如何编码,因此对我的帮助非常感谢。
Thanks 谢谢
I will suggest to create DTOs for Header and Lines, have List<Line>
available in the Header DTO, you can retrieve data as you want and prepare List<Header>
from this data and use sterilizer to convert it into XML. 我建议创建用于标题和行的DTO,在标题DTO中提供
List<Line>
,您可以根据需要检索数据并从该数据中准备List<Header>
,然后使用消毒器将其转换为XML。
You can have first your XSD and you can generate DTOs from XSD using XSD.exe or XSD2Code , use this generated DTO that will give you fine control on how XML should be generated. 您可以先拥有XSD,然后可以使用XSD.exe或XSD2Code从XSD生成DTO,使用此生成的DTO可以很好地控制应如何生成XML。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.