![](/img/trans.png)
[英]How to pass an action to be performed on some XElement as a parameter?
[英]Pass List as XElement to be used as XML Datatype parameter
我在SQL Server中有一個存儲過程
CREATE PROCEDURE ParseXML (@InputXML xml)
輸入參數的數據類型是“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)
現在,如何將以下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);
}
問題代碼
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);
必需的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);
讀
您可以將列表轉換為XML片段,如下所示:
IEnumerable<XElement> el = list.Select(i =>
new XElement("BankAccountDTOForStatus",
new XElement("BankAccountID", i.BankAccountID),
new XElement("Status", i.Status)
));
然后你可以把它變成一個XElement:
XElement root = new XElement("root", el);
現在你可以將它作為參數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
你需要這樣的東西:
XDocument
XDocument.Root
傳遞給Linq-to-SQL數據上下文中的ParseXML方法 所以你的代碼是這樣的:
// define input XML - e.g. load from file or whatever
string xmlInput =
@"<ArrayOfBankAccountDTOForStatus>
<BankAccountDTOForStatus>
<BankAccountID>2</BankAccountID>
<Status>FrozenFA</Status>
</BankAccountDTOForStatus>
<BankAccountDTOForStatus>
<BankAccountID>3</BankAccountID>
<Status>FrozenSB</Status>
</BankAccountDTOForStatus>
</ArrayOfBankAccountDTOForStatus>";
// convert that into a XDocument
XDocument doc = XDocument.Parse(xmlInput);
// using your DataContext - call ParseXML
using (DataClasses1DataContext ctx = new DataClasses1DataContext())
{
var result = ctx.ParseXML(doc.Root);
}
就是這樣! 現在您的XML被傳遞給存儲過程並在那里處理。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.