Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading an XML file into a String and updating Database

Status
Not open for further replies.

DilipKS

Programmer
Mar 8, 2004
30
CH
Hi
I got really one frustrating problem and am struggling with it. I got an XML , I just want to read it in a String and need to insert in Database.
But the problem is I tried all ways but whenever I read XML into a String , I get \ inserted before every " . e.g. whereever attrib values r there .
So now when i tried to insert in Database it gives Error.

any idea how do i do ??
do i need any encoding ?



// Testing Begins
string strXMLMsg;
XmlDocument doc = new XmlDocument();
doc.Load("C:\\Temp\\Test.xml");
System.IO.StringWriter stream = new System.IO.StringWriter() ;
XmlTextWriter writer = new XmlTextWriter(stream) ;
writer.Formatting = Formatting.Indented;
doc.Save(writer);
strXMLMsg = stream.GetStringBuilder().ToString() ;



Sample XML
<Execution>
<SubWave>NoValue</SubWave>
<ExecRefID Source="ukagrq01">1316</ExecRefID>
<xftag Source="L">3084PKRJ04</xftag>
<SecurityID IDSource="RIC">OML.L</SecurityID>
<BookingDetails BookingService="FTS_UK"/>
<BargainConditions>
<AT/>
<RP/>
</BargainConditions>
</Execution>

In String Output am getting as
"<Execution>
<SubWave>NoValue</SubWave>\r\n <ExecRefID Source=\"ukagrq01\">1303</ExecRefID>\r\n <xftag Source=\"L\">2062KO8804</xftag>\r\n <SecurityID IDSource=\"RIC\">OML.L</SecurityID>\r\n <BookingDetails BookingService=\"FTS_UK\" />\r\n <BargainConditions>\r\n <AT />\r\n <RP />\r\n </BargainConditions>\r\n</Execution>"


Desired Ouput -

"<Execution><SubWave>NoValue</SubWave><ExecRefID Source="ukagrq01">1316</ExecRefID><xftag Source="L">3084PKRJ04</xftag>
<SecurityID IDSource="RIC">OML.L</SecurityID>
<BookingDetails BookingService="FTS_UK"/>
<BargainConditions><AT/><RP/>
</BargainConditions>
</Execution>"
 
The following lines will do the job and with the MessageBox.Show() you will see all the file right displayed as a string:
Code:
XmlDocument doc = new XmlDocument();
XmlTextReader reader = new XmlTextReader("C:\\myfile.xml");
reader.WhitespaceHandling = WhitespaceHandling.None;
doc.Load(reader);
string all= doc.InnerXml;
MessageBox.Show(all);
Now , the next step is how to insert/update this value into a column in DB.
1. You can pass it to a stored procedure and in which you have insert or update statement
2. You can create a DataAdapter, build the insert or update command using Command.Parameters and call ExecuteNoQuery.
-obislavu-
 
I agree when you display it , it doesnt show "\" but when I try to update into DB using below its giving error
Original Msg contains string which has "\" wherever double quotes are there .
-

string strCmdText = "UPDATE system_exceptions SET OriginalMessage = '" + strOriginalMsg + "' WHERE ExcpMsgID="+myChildDataSet.Tables[0].Rows[0]["ExcpMsgID"].ToString();
OdbcCommand updDBCommand = new OdbcCommand(strCmdText,myConnection2);
updDBCommand.ExecuteNonQuery();
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top