将List作为XElement传递以用作XML Datatype参数

LCJ*_*LCJ 5 .net c# xml sql-server linq-to-sql

我在SQL Server中有一个存储过程

CREATE PROCEDURE ParseXML (@InputXML xml)
Run Code Online (Sandbox Code Playgroud)

输入参数的数据类型是"xml".

在LINQ to SQL生成的存储过程代码中,输入参数是System.Xml.Linq.XElement

[global::System.Data.Linq.Mapping.FunctionAttribute(Name="dbo.ParseXML")]
public ISingleResult<ParseXMLResult> ParseXML([global::System.Data.Linq.Mapping.ParameterAttribute(Name="InputXML", DbType="Xml")] System.Xml.Linq.XElement inputXML)
Run Code Online (Sandbox Code Playgroud)

现在,如何将以下List传递给ParseXML方法以使存储过程正常工作?

编辑:

阅读答案后 - 下面列出了另一种解决方案

XElement root = new XElement("ArrayOfBankAccountDTOForStatus",
new XAttribute(XNamespace.Xmlns + "xsi", "http://www.w3.org/2001/XMLSchema-instance"),
new XAttribute(XNamespace.Xmlns + "xsd", "http://www.w3.org/2001/XMLSchema"));


foreach (var element in bankAccountDTOList)
{

 XElement ex= new XElement("BankAccountDTOForStatus", 
                      new XElement("BankAccountID", element.BankAccountID),
                      new XElement("Status", element.Status));


 root.Add(ex);
} 
Run Code Online (Sandbox Code Playgroud)

问题代码

        string connectionstring = "Data Source=.;Initial Catalog=LibraryReservationSystem;Integrated Security=True;Connect Timeout=30";
        var theDataContext = new DBML_Project.MyDataClassesDataContext(connectionstring);

        List<DTOLayer.BankAccountDTOForStatus> bankAccountDTOList = new List<DTOLayer.BankAccountDTOForStatus>();
        DTOLayer.BankAccountDTOForStatus presentAccount1 = new DTOLayer.BankAccountDTOForStatus();
        presentAccount1.BankAccountID = 5;
        presentAccount1.Status = "FrozenF13";

        DTOLayer.BankAccountDTOForStatus presentAccount2 = new DTOLayer.BankAccountDTOForStatus();
        presentAccount2.BankAccountID = 6;
        presentAccount2.Status = "FrozenF23";
        bankAccountDTOList.Add(presentAccount1);
        bankAccountDTOList.Add(presentAccount2);

        //theDataContext.ParseXML(inputXML);
Run Code Online (Sandbox Code Playgroud)

必需的XML结构

在此输入图像描述

注意:此XML用于某些操作,而不是直接以XML格式存储在数据库中.我需要编写一个select列表来列出XML中的数据.

存储过程逻辑

DECLARE @MyTable TABLE (RowNumber int, BankAccountID int, StatusVal varchar(max))

INSERT INTO @MyTable(RowNumber, BankAccountID,StatusVal)

SELECT ROW_NUMBER() OVER(ORDER BY c.value('BankAccountID[1]','int') ASC) AS Row,
    c.value('BankAccountID[1]','int'),
    c.value('Status[1]','varchar(32)')
FROM
    @inputXML.nodes('//BankAccountDTOForStatus') T(c);
Run Code Online (Sandbox Code Playgroud)

  1. 如何使用Linq to SQL将对象序列化并将对象保存为Xml

  2. 当XElements具有相同名称时,如何使用LINQ查询来获取XElement值

  3. 使用XML数据库字段的Linq-to-SQL - 为什么这样做?

  4. http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=176385

Iva*_*n G 7

您可以将列表转换为XML片段,如下所示:

        IEnumerable<XElement> el = list.Select(i => 
                            new XElement("BankAccountDTOForStatus", 
                              new XElement("BankAccountID", i.BankAccountID),
                              new XElement("Status", i.Status)
                            ));
Run Code Online (Sandbox Code Playgroud)

然后你可以把它变成一个XElement:

        XElement root = new XElement("root", el);
Run Code Online (Sandbox Code Playgroud)

现在您可以将它作为参数inputXML传递给ParseXML,它是XElement类型.在存储过程句柄中它像这样:

DECLARE @InputXML NVARCHAR(1024) = N'
<root>
 <BankAccountDTOForStatus>
     <BankAccountID>2</BankAccountID>
     <Status>FrozenFA</Status>
 </BankAccountDTOForStatus>
 <BankAccountDTOForStatus>
     <BankAccountID>3</BankAccountID>
     <Status>FrozenSB</Status>
 </BankAccountDTOForStatus>
</root>'

DECLARE @handle INT

EXEC sp_xml_preparedocument @handle OUTPUT, @InputXML

SELECT  *
FROM    OPENXML(@handle, '/root/BankAccountDTOForStatus', 1)
WITH    (
            BankAccountID INT 'BankAccountID/text()',
            Status VARCHAR(128) 'Status/text()'
)

EXEC sp_xml_removedocument @handle
Run Code Online (Sandbox Code Playgroud)


归档时间:

查看次数:

5325 次

最近记录:

13 年,3 月 前