Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse xml from database

Tags:

c#

xml

asp.net

Currently I have xml stored in a ms sql dbase and not a file. Here's a small sample.

<NewDataSet>
<Table1>
<billTo_lastName>asdf</billTo_lastName>
<orderAmount>160.00</orderAmount>
<billTo_street1>asdf</billTo_street1>
<card_accountNumber>############1111</card_accountNumber>
</Table1>
</NewDataSet>

Currently I'm returning the result in a Datable.

What would be the best way to parse the above and display it on a page. The page display is just for informational review. No additional processing will be done to the xml.

I would like the page to display something along these lines.

billTo_lastName: asdf
orderAmount: 160.00
etc.
like image 744
Prescient Avatar asked Mar 11 '13 21:03

Prescient


People also ask

How parse XML in SQL?

First, the sp_xml_preparedocument stored procedure parses the XML document. The parsed document is a tree representation of the nodes (elements, attributes, text, and comments) in the XML document. OPENXML then refers to this parsed XML document and provides a rowset view of all or parts of this XML document.

How do you parse an XML file?

To parse XML documents, use the XML PARSE statement, specifying the XML document that is to be parsed and the processing procedure for handling XML events that occur during parsing, as shown in the following code fragment.

How parse XML in PL SQL?

First approach: Load the XML file into an XML table and then parse it. First, create a table in Oracle that includes a column with data type XMLTYPE. For example, use the following code to create the table: CREATE TABLE xml_tab ( File_name varchar2(100), xml_data XMLTYPE );


2 Answers

Depends on how you want to process it. One way is to create an XmlDocument and then call LoadXml:

// get the data from the data table, into a string.

// then create an XML document and load the string
var doc = new XmlDocument();
doc.LoadXml(dataString);

If you want to use Linq-to-Xml, you'd create an XElement:

var element = XElement.Load(dataString);
like image 52
Jim Mischel Avatar answered Sep 22 '22 21:09

Jim Mischel


Thanks to Jim for pointing me in the right direction. XmlDocuments LoadXml works just fine. I then was able to put that into an XmlNodeList and selected the top node. Did XmlNode and looped through the child nodes.

    DataTable dt = sqlselect(sqlQuery, parameter);
    var doc = new XmlDocument();
    doc.LoadXml(dt.Rows[0]["ua_post"].ToString());

    XmlNodeList nl = doc.SelectNodes("NewDataSet");
    XmlNode root = nl[0];

    foreach (XmlNode xnode in root.ChildNodes[0])
    {
        string name = xnode.Name;
        string value = xnode.InnerText;
        string nv = "<b>" + name + ":</b> " + value;
        Label1.Text += nv + " <br />" + Environment.NewLine;
    }

And I get a simple display on a page.

    billTo_lastName: asdf 
    orderAmount: 160.00 
    billTo_street1: asdf 
    card_accountNumber: ############1111 
like image 29
Prescient Avatar answered Sep 24 '22 21:09

Prescient