Incorrect Format while adding data into a XML file.

R

reeya

Hello all.

I am writing some "CART" data into an xml and it is not coming in the
right format. I have an dataset which has an orderID element and a
product element. The product element has a productid element.

Now once i get the cart, i take this data and using the dataset, i
write it into an xml file.

the format it "should appear" in is:
<OrderInformation>
<Order>
<Id>0</Id>
<Product>
<ProductId>21</ProductId>
</Product>
<Product>
<ProductId>22</ProductId>
</Product>
</Order>
</OrderInformation>

but its coming as

<OrderInformation>
<Order>
<Id>0</Id>
</Order>
<Product>
<ProductId>21</ProductId>
</Product>
<Product>
<ProductId>22</ProductId>
</Product>
</OrderInformation>

obviously i am adding it in an incorrect order ..

my code is:

string fileLocation =
(string)ConfigurationSettings.AppSettings["ShoppingCart.Location"];
ShoppingCart shoppingCart = new ShoppingCart();
ShoppingCart.ProductRow productRow = null;

int noOfItems = 0;
if (cart != null)
{
CartLineItem[] cLineItems = cart.CartLineItemArray;
if (cLineItems != null)
{
foreach(CartLineItem lineItem in cLineItems)
{
shoppingCart.DataSetName =
"OrderInformation"; shoppingCart.Order.AddOrderRow(noOfItems.ToString());
productRow = shoppingCart.Product.NewProductRow();
productRow.ProductID = lineItem.ProductID;
shoppingCart.Product.AddProductRow(productRow); noOfItems++;
}
if (shoppingCart.Order.Rows.Count > 0)
{
shoppingCart.WriteXml(fileLocation);
}
}
}

Can anyone please advise, what am i doing wrong here..

Thanx very much.
Reeya
 
E

Eric Cadwell

Do you need to populate the DataSet? You could write the XML directly.

If you need the data in the DataSet for other purposes then it appears that
you would need to set up a relationship between Orders and Order Details.
Something like this.
dsOrder.Relations.Add("Order Line Items",
dsOrder.Tables["Orders"].Columns["OrderID"], dsOrder.Tables["Order
Details"].Columns["OrderID"])

After doing that I was able to output data from DataSet.WriteXML:

<Order>
<OrderDetails>
<OrderID>10255</OrderID>
<ProductID>16</ProductID>
</Order>
<OrderDetails>
<OrderID>10255</OrderID>
<ProductID>25</ProductID>
</Order>
</OrderInformation>

You may be able to do more by complex formatting by editing the DataSet
schema. Touch back with more detail and I'll try to put a sample together.

-Eric
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top