I have a stored procedure in SQL Server
CREATE PROCEDURE ParseXML (@InputXML xml)
The data type for input parameter is “xml”.
In the LINQ to SQL generated code for the stored procedure the input parameter is 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)
Now, how can I pass the following List to the ParseXML method to make the stored procedure work?
EDIT:
After reading the answer – another solution is listed below
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);
}
CODE In Question
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);
Required XML Structure

Note: This XML is used for some operations, not for directly storing in database as XML. I need to write a select query that will list the data from the XML.
Stored Procedure Logic
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);
READING
You can turn your list into an XML fragment like this:
Then you can turn it into an XElement:
Now you can just pass it to ParseXML as parameter inputXML which is of type XElement.
In stored procedure handle it like this: