convert string to xml and pass it to a stored proc [Resolved]

Posted by Praveen7k under VB.NET on 6/8/2011 | Points: 10 | Views : 15928 | Status : [Member] | Replies : 5
Hi,

I have a screen where user enters id,firstName ,lastName, DoB these all are strings.

i have a package it accepts xmltype in parameter like

create or replace procedure Search (
P_parameters in As XMLType
P_data out As Varchar2..............

so i have to convert user entered values into XMLType and pass to the package
db.parameters("P_parameters", Oracle.dataaccess.client.Dbtype.XMLType, ,, direction.pparameter.input) like this or anyother way.

i have pass all the user entered values as single parameter.........urgent please thanks




Responses

Posted by: Ndebata on: 6/9/2011 [Member] Starter | Points: 50

Up
0
Down

Resolved
Hi
Lets say you have following data from which you want to create an xml.

string _FirstName = "Hello";
string _LastName = "Again";
string _DOB = "1/1/2011";
string _Information = "Just came";

I have string called _xmlstring where i will store my generated xml.
The o/p of any of the methods bellow will be

<MyData>
<FirstName>Hello</FirstName>
<LastName>Again</LastName>
<DOB>1/1/2011</DOB>
<Info>Just came</Info>
</MyData>

The xml has been created in three approaches bellow.
Approach 1 (Using XmlDocument and XmlNode namespace using System.Xml)


XmlDocument xdoc = new XmlDocument();
XmlNode pnode = xdoc.CreateElement("MyData");
xdoc.AppendChild(pnode);
XmlNode namenode = xdoc.CreateElement("FirstName");
namenode.InnerXml = _FirstName;
xdoc.ChildNodes[0].AppendChild(namenode);
namenode = xdoc.CreateElement("LastName");
namenode.InnerXml = _LastName;
xdoc.ChildNodes[0].AppendChild(namenode);
namenode = xdoc.CreateElement("DOB");
namenode.InnerXml = _DOB;
xdoc.ChildNodes[0].AppendChild(namenode);
namenode = xdoc.CreateElement("Info");
namenode.InnerXml = _Information;
xdoc.ChildNodes[0].AppendChild(namenode);
//Assign the xml string to string variable;
string _xmlstring = xdoc.InnerXml;

Approach 2 (Using XmlDocument and XmlElement namespace using System.Xml;)


XmlDocument xdoc = new XmlDocument();
XmlElement root = xdoc.CreateElement("MyData");
xdoc.AppendChild(root);
XmlElement chnode = xdoc.CreateElement("FirstName");
chnode.InnerXml = _FirstName;
root.AppendChild(chnode);
chnode = xdoc.CreateElement("LastName");
chnode.InnerXml = _LastName;
root.AppendChild(chnode);
chnode = xdoc.CreateElement("DOB");
chnode.InnerXml = _DOB;
root.AppendChild(chnode);
chnode = xdoc.CreateElement("Info");
chnode.InnerXml = _Information;
root.AppendChild(chnode);
//Assign the xml string to string variable;
string _xmlstring = xdoc.InnerXml;

Approach 3 (Using XElement namespace using System.Xml.Linq;)


XElement xe = new XElement("MyData",
new XElement("FirstName", _FirstName),
new XElement("LastName", _LastName),
new XElement("DOB", _DOB),
new XElement("Info", _Information));
//Assign the xml string to string variable;
_xmlstring = xe.ToString();


Thanks,
Debata

Praveen7k, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ndebata on: 6/8/2011 [Member] Starter | Points: 25

Up
0
Down
Hi
Praveen may i know how you want your xml to look like, I mean structure?
You can create XML in many ways,
The simplest one is using XElement.

Thanks,
Debata


Praveen7k, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Susanthampy on: 6/8/2011 [Member] [MVP] Bronze | Points: 25

Up
0
Down
Hi,

Try this,

http://stackoverflow.com/questions/1698153/calling-a-stored-procedure-with-xml-datatype

Regards,
Susan

Praveen7k, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Praveen7k on: 6/8/2011 [Member] Starter | Points: 25

Up
0
Down
i want using xmldocument and xmlelement....

Praveen7k, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Praveen7k on: 6/9/2011 [Member] Starter | Points: 25

Up
0
Down
thanks...a lot..

Praveen7k, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response