简体   繁体   English

使用LINQ将两个表中的SQL数据输出到XML

[英]Using LINQ to output SQL data from two tables into XML

i am very new to this. 我对此很陌生。 Hope someone could help me suggest how to improve the code. 希望有人可以帮助我建议如何改进代码。

I have two tables where i need to get the SQL data and ouput it into XML format. 我有两个表,我需要在其中获取SQL数据并将其输出为XML格式。 I am using LINQ method. 我正在使用LINQ方法。 Below how the code looks like. 下面的代码看起来像。

#region Database XML Methods

private static void CreateDatabaseXml(string path)
{
    tbchrDataContext db = new tbchrDataContext();
    XDocument doc = new XDocument(
        // XML Declaration
        new XDeclaration("1.0", "utf-8", "yes"),
        // XML Root element to 3rd in nest
        new XElement(ns + "WMS",
        new XElement(ns + "Order",
        new XElement(ns + "Header", from a in db.T_ORDER_DETAILs
                                    select new XElement(ns + "RARefNum", a.RARefNum), 
                                    new XElement (ns + "WMSCategory", from b in db.T_ORDER_HEADERs select b.Customer),
                                    new XElement (ns + "CustomerID", from a in db.T_ORDER_DETAILs select a.SupplierName)))) );

    #endregion
    doc.Save(path);
}

And below how is the output of XML looks like. 下面是XML输出的样子。

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<WMS xmlns="http://blog.cripperz.sg">
  <Order>
    <Header>
      <RARefNum>RASO000001</RARefNum>
      <RARefNum>RASO000001</RARefNum>
      <WMSCategory>ESSVMI</WMSCategory>
      <CustomerID>nVidianVidia</CustomerID>
    </Header>
  </Order>
</WMS>

Ultimately i wanted to achieve the below XML, some of the data grab from SQL is from two separate tables in one XML nest / element. 最终,我想实现以下XML,从SQL抓取的一些数据来自一个XML嵌套/元素中的两个单独的表。

<?xml version="1.0" encoding="utf-8"?>
<WMS>
    <Order>
        <Header>
            <RARefNum>RASO000001</RARefNum>
            <WMSCategory>ESSVMI</WMSCategory>
            <CustomerID>nVidia</CustomerID>
            <CreationDate>2013-12-02 06:29:50</CreationDate>
            <OrderDate>2013-12-02 06:29:50</OrderDate>
            <ExpectedShippedDate>2013-12-02 06:29:50</ExpectedShippedDate>
            <LastShippedDate>2013-12-02 06:29:50</LastShippedDate>
            <CustomerOrderReference>nVidia9338</CustomerOrderReference>
            <CustomerShipmentNo>81475721</CustomerShipmentNo>
            <CustomerSONo>SO982733</CustomerSONo>
            <CustomerInvoiceNo>INV987373</CustomerInvoiceNo>
            <CustomerReference1>nVidia 1</CustomerReference1>
            <CustomerReference2/>
            <WMSReference1>Emp 1</WMSReference1>
            <WMSReference2>Emp 2</WMSReference2>
            <ShipmentNo>IWU997872</ShipmentNo>
            <DocumentNo>KK98764394</DocumentNo>
            <Transportation>
                <Mode>Freight</Mode>
                <VehicleType/>
            </Transportation>
            <Carrier>
                <ID>Fedex</ID>
                <Name>Fedex SG</Name>
                <Address>Changi Singapore</Address>
                <Country/>
                <PostalCode/>
                <Contact>
                    <Sequence/>
                    <Person/>
                    <Email/>
                    <DID/>
                    <Handphone/>
                </Contact>
            </Carrier>
            <Consignee>
                <ID>ABC</ID>
                <Name>ABC Corp</Name>
                <Address>Jurong West, Singapore</Address>
                <Country/>
                <PostalCode/>
                <Contact>
                    <Sequence/>
                    <Person/>
                    <Email/>
                    <DID/>
                    <Handphone/>
                </Contact>
            </Consignee>
            <Containers/>
        </Header>
        <Details>
            <Detail>
                <LineNo>1</LineNo>
                <SKU>SKU0001</SKU>
                <SKUDescription>SKU 0001</SKUDescription>
                <Package>50</Package>
                <OrderedQty>600.000</OrderedQty>
                <PickedQty>600.000</PickedQty>
                <PickedDate>2013-12-02 06:35:09</PickedDate>
                <ShippedQty>600.000</ShippedQty>
                <ShippedDate>2013-12-02 06:35:09</ShippedDate>
                <ManufactoryDate>2013-12-02 06:35:09</ManufactoryDate>
                <ExpiryDate>2014-12-02 06:35:09</ExpiryDate>
                <FIFODate>2013-06-02 06:35:09</FIFODate>
                <CustomerLotRef1>nVidia 2093</CustomerLotRef1>
                <CustomerLotRef2>nVidia 2099</CustomerLotRef2>
                <LineReference1>10</LineReference1>
            </Detail>
            <Detail>
                <LineNo>2</LineNo>
                <SKU>SKU0002</SKU>
                <SKUDescription>SKU 0002</SKUDescription>
                <Package>50</Package>
                <OrderedQty>100.000</OrderedQty>
                <PickedQty>100.000</PickedQty>
                <PickedDate>2013-12-02 06:35:09</PickedDate>
                <ShippedQty>100.000</ShippedQty>
                <ShippedDate>2013-12-02 06:35:09</ShippedDate>
                <ManufactoryDate>2013-12-02 06:35:09</ManufactoryDate>
                <ExpiryDate>2014-12-02 06:35:09</ExpiryDate>
                <FIFODate>2013-06-02 06:35:09</FIFODate>
                <CustomerLotRef1>nVidia 2193</CustomerLotRef1>
                <CustomerLotRef2>nVidia 2199</CustomerLotRef2>
                <LineReference1>10</LineReference1>
            </Detail>
        </Details>
    </Order>
</WMS>

Is there a better way to code it? 有更好的编码方式吗?

I dont know if this is better but I would probably make some variables instead of calling the linq in the xml. 我不知道这是否更好,但我可能会做一些变量而不是在xml中调用linq。 Then you can just call the variable in the xml document. 然后,您可以仅在xml文档中调用变量。

Something like this maybe: 可能是这样的:

 private static void CreateDatabaseXml(string path)
{
    tbchrDataContext db = new tbchrDataContext();

    var rARefNum = db.T_ORDER_DETAILs.Select(i => i.RARefNum).Single();
    var customer = db.T_ORDER_HEADERs.Select(i => i.Customer).Single();
    var supplierName = db.T_ORDER_DETAILs.Select(i => i.SupplierName).Single();

    XDocument doc = new XDocument(
        // XML Declaration
        new XDeclaration("1.0", "utf-8", "yes"),
        // XML Root element to 3rd in nest
        new XElement(ns + "WMS",
        new XElement(ns + "Order",
        new XElement(ns + "Header", new XElement(ns + "RARefNum", rARefNum), 
                                    new XElement (ns + "WMSCategory", customer),
                                    new XElement (ns + "CustomerID", supplierName)))) );

    #endregion
    doc.Save(path);
}
  • In sql server support lot of xml format outputs. 在sql server中支持很多xml格式的输出。
  • This query returns a xml document from two tables. 该查询从两个表返回一个xml文档。 Maybe you are using linq, then after completing your sql query, you execute the query from linq. 也许您使用的是linq,然后在完成sql查询后,从linq执行查询。

    select table1.column1, table2.column2 from table1 inner join table2 on table2.table1_id = table1.Id for xml auto 从table2的table1内部联接table2中选择table1.column1,table2.column2进行xml auto

Check this link . 检查此链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM