2

Whats the best way of building xml document based on few oracle queries in C#

select orderID,qty,orderDate,deliveryDate from Orders

<orders>
   <order>
     <orderID>1</orderID>
     <qty>10</qty>
     <orderDate>22-Jan-2012</orderDate>
     <deliveryDate>25-Jan-2012</deliveryDate>


   </order> 
<order>
     <orderID>2</orderID>
     <qty>10</qty>
     <orderDate>22-Jan-2012</orderDate>
     <deliveryDate>25-Jan-2012</deliveryDate>


   </order> 
</orders>

Please advice

1
  • Your XML seems to contradict the question posed. Are you looking to turn a query to XML or the result of your query to XML? Commented Mar 9, 2012 at 2:08

3 Answers 3

1

You may use XElement.
Code Example:

using (SqlConnection con = new SqlConnection(ConnectionString))
{
    con.Open();
    using(SqlCommand command = new SqlCommand("select orderID,qty,orderDate,deliveryDate from Orders", con))
    {
        SqlDataReader reader = command.ExecuteReader();

        XElement root = new XElement("Orders");
        while(reader.Read())
        {
            root.AddFirst(
                new XElement("Order", 
                from i in Enumerable.Range(0, reader.FieldCount)
                    select 
                        new XElement(reader.GetName(i), reader.GetValue(i))
                )
            );
        }
        root.Save(Console.Out);

    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Please refer to this example: http://support.microsoft.com/kb/301271

Except instead of SqlConnection & SqlDataAdapter, use OracleConnection & OracleDataAdapter.

Comments

0

You can do it in the oracle query itself.

Please find the below link

https://forums.oracle.com/forums/thread.jspa?threadID=1034099

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.