简体   繁体   English

C#从多个数据集中创建XML文件(一个用于详细信息)

[英]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.

相关问题 c# 从文件夹中的多个 csv 文件中检索详细信息 - c# Retrieve details from more than one csv file in a folder 显示具有多个数据集的rdlc报告-c# - Display rdlc report with more than one dataset -c# 通过XSLT将超过1个动态xml文件转换为C#中的一个xml / resx文件 - Transform more than 1 dynamic xml Files via XSLT into one xml/resx file in C# c#如何从数据集或XML文件创建类型化的数据集? - c# how to create a typed dataset from a dataset or a XML file? 使用C#解析一个包含多个键值的xml文件 - parsing a xml file which contains more than one keyvalues using c# c# - 从具有多个值的 XML 节点获取值 - c# - Get values from XML Nodes with more than one value 如何使用多个属性创建ac#枚举 - How to create a c# enumeration with more than one property 选择多个XML文件时,将XML文件保存在DataSet数组中,并发生System.NullReferenceException - Saving XML files in DataSet array and System.NullReferenceException occurs when selecting more than one XML file 在 C# 中创建 MS Teams 团队 - 不止一个所有者 - Create MS Teams team in C# - more than one owner 如何在Crystal Report C#中创建多个报告? - How to create more than one reports in a crystal report c#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM