1

I have a web service which expects input in xml format and input needs to be encoded. The environment i am working on is .NET 4.5 and code behind is c#. I have to include the encoded xml input in the SOAP request and send it to the service which sends back the output as SOAP response. I could send SOAP request and receive the response but i am having trouble in sending the xml input. The input involves data from oracle. For example i have a table customer

Customer

CustomerName | key       |  Value    |  
______________________________________
AAA          | Grocery   |  10       |
AAA          | Clothing  |  null     |  

In the above table if the value has null i need to refer to the other table with foreign key = "key" and get the value from the other table.

I want to build the xml as i get value from query. So if i query the customer table by passing "AAA", first i have to read the grocery and the value 10 and the xml should look like

<customer>
 <Grocery>10</Grocery>
</customer>

Then i read the next row i see clothing then the value of clothing is null, so i would pass the orderType value (Clothing) to another table and get orderValue and append to the above xml to get the final xml

<customer>
  <Grocery>10</Grocery
  </Clothing>value from other table</Clothing>
</customer>

It is like building key value pair by getting values from database. For now i have code that reads all the values of a row using datareader.getValues and store it in array. I tried to create xml from the array but the problem is the xml is not getting added but is written overwritten.

using (OracleConnection conn = new OracleConnection(dbConnectionString))
        {
            conn.Open();
            using (OracleCommand comm = new OracleCommand(query, conn))
            {
                using (OracleDataReader dr = comm.ExecuteReader())
                {
                    while (dr.Read())
                    {
                        Object[] values = new Object[dr.FieldCount];
                        int fieldCount = dr.GetValues(values);


                        XElement xmlInput =
                                      new XElement("Document",
                                          new XElement(values[1].ToString(), values[0].ToString())
                                          );

                            XElement xmlTree = new XElement("Document");

                                foreach (XElement e1 in xmlInput.Elements())
                                {
                                    xmlTree.Add(new XElement("Document",
                                        new XElement(columnName,rowValue)));
                                }
                                xmlInput.Add(xmlTree);
                         }      
                    }
                }

            } 

So how do i append the xml as i keep reading hey value pair in data reader?

1 Answer 1

4

Assuming that you have an array of key value pairs named for example 'allKeyValuePairs', you can easily create the XML structure that you posted by using this code:

XElement xmlDocument = new XElement("Customer");

foreach (KeyValuePair<string, string> singlePair in allKeyValuePairs)
{
    xmlDocument.Add(new XElement(singlePair.Key, singlePair.Value));
}

The resulting XML will have structure like this:

<Customer>
  <Key1>Value1</Key1>
  <Key2>Value2</Key2>
  <Key3>Value3</Key3>
  ....
</Customer>

You were creating more elements with the same name 'Document', and then kept overwriting them by adding them to the same root element in a loop.

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

2 Comments

Thanks, that make sense. But is there a way to create xml as we read the values?
Yes, just call xmlDocument.Add to add new elements as you read new values. Only make sure that xmlDocument is referring to the same object you want to add values to.

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.