'Passing XMLDocument to Stored Procedure
My current setup is as follows:
Client Javascript JSON stringified object is passed to a server function as so
Client:
var requestObject = JSON.stringify(clientObject);
$.ajax({
url: 'ServerClass.aspx/ServerFunction',
data: requestObject,
dataType: "json",
contentType: "application/json; charset=utf-8",
cache: false,
context: document.body,
type: 'POST',
success: saveSuccessfulFunction
});
Server:
[WebMethod(EnableSession = true)]
public static int SaveAllReportOptions(string requestObject)
{
XmlDocument xdoc = JsonConvert.DeserializeXmlNode("{\"root\":" + clientObject + "}", "roots");
DBClass.Save(xdoc);
}
public int Save(XmlDocument clientObject)
{
SqlCommand dCmd = new SqlCommand("MyStoredProcedure", conn);
dCmd.CommandType = CommandType.StoredProcedure;
dCmd.Parameters.AddWithValue("@objectXML", SqlDbType.Xml).Value = clientObject.InnerXml;
SqlParameter returnValue = dCmd.Parameters.Add("@ret", SqlDbType.Int);
returnValue.Direction = ParameterDirection.ReturnValue;
conn.Open();
dCmd.ExecuteNonQuery();
conn.Close();
int i = Convert.ToInt32(dCmd.Parameters["@ret"].Value);
return i;
}
The stored procedure successfully extracts the different nodes/attributes from the XMLDocument passed in and goes on to perform the relevant update/insert commands.
SELECT tab.col.value('att1[1]','NCHAR(10)') as attribute1,
tab.col.value('att2[1]','INT') as attribute2...
FROM @objectXML.nodes('/roots/root') AS tab(col)
My issue isn't with the above code but with certain do's and don'ts observed from various online/in house software coding standards.
Source 1
X DO NOT use XmlNode or XmlDocument to represent XML data. Favor using instances of IXPathNavigable, XmlReader,XmlWriter, or subtypes of XNode instead. XmlNode and XmlDocument are not designed for exposing in public APIs.
Source 2
✓ DO use XmlReader, IXPathNavigable, or subtypes of XNode as input or output of members that accept or return XML. Use these abstractions instead of XmlDocument, XmlNode, or XPathDocument, because this decouples the methods from specific implementations of an in-memory XML document and allows them to work with virtual XML data sources that expose XNode, XmlReader, or XPathNavigator.
Source 3
X DO NOT subclass XmlDocument if you want to create a type representing an XML view of an underlying object model or data source.
Basically, I would like to know if it is safe and reliable enough to use XMLDocument for the purpose detailed above. And if not, if there are any alternatives which would be better suited for my scenario.
Solution 1:[1]
I used this approach - For e.g. You have customer object with multiple orders ( child classes)
1. At client side - after assigning values in both the classes
customerObject = JSON.stringify(cls_m_Customer, function(k, v){ return v === "" ? "" : v });
2. At server side -
JavaScriptSerializer jss = new JavaScriptSerializer();
cls_m_Customer objCustomer = new cls_m_Customer();
objCustomer = jss.Deserialize<cls_m_Customer>(customerData);
3. At Business Class - create two dictionaries for customer and orders classes and add these tables to dataset and then convert ds into xml like -
Dictionary<string, cls_m_Customer> dic_Customer = new Dictionary<string, cls_m_Customer>();
dic_Customer.Add("1", this);
DataSet ds = new DataSet();
DataTable dtCustomer = DictionaryToDataTable.ConvertTo<cls_m_Customer>(dic_Customer, "Customer");
DataTable dtOrders = DictionaryToDataTable.ConvertTo<cls_m_Order>(this._Order, "Orders");
ds.Tables.Add(dtCustomer);
ds.Tables.Add(dtOrders);
DataRelation drQ = new DataRelation("Customer Related Orders", dtCustomer.Columns["_CustomerID"], dtOrders.Columns["_CustomerID"]);
drQ.Nested = true;
ds.Relations.Add(drQ);
customerXml = ds.GetXml();
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Bharat Bhushan Sharma |