How can I update these XML values if they have been updated, when parsing XML in C#?

I am parsing XML to update if changes are made. A check is done to see if the order already exists, and if so, doesn't add the invoice rows. However, how can I go about modifying the values if they are updated? Since these are nodes within nodes and whatnot, I have also added the XML example here, with the included invoiceRows.


<?xml version="1.0" encoding="UTF-8"?>

-<InvoiceOrder xmlns:xsd="" xmlns:xsi="">

<OrderId xmlns="">88844165</OrderId>

<CustomerId xmlns="">7</CustomerId>

<CustomerName xmlns="">Test Company</CustomerName>

-<Addresses xmlns="">


<Street>Chestnut Avenue</Street>


<PostalCode>SO53 ABC</PostalCode>

<Name>Test House</Name>





<Street> 56 Test Street</Street>

<PostalCode>BL4 8BE</PostalCode>

<Name>Mrs Hughes</Name>





<OrderStatus xmlns="">Invoiced</OrderStatus>

<DateOrdered xmlns="">2015-11-02</DateOrdered>


<IncludeVAT xmlns="">True</IncludeVAT>

-<Currency xmlns="">



<TypeOfSaleId xmlns="">-100</TypeOfSaleId>

-<InvoiceRows xmlns="">





<Name>CADENCE RAD</Name>


















Open in new window

C# Code

bool orderidfound = false; //checks whether order already exists

                                                    for (int k = 0; k < po.Length; k++)
                                                        if (po[k].OrderId == Convert.ToInt32(fileref.importList["OrderId"].InnerText)) //if order number is equal
                                                            orderidfound = true;

                                                    if (orderidfound == false) //ensures duplicate invoice rows are not added

                                                        newInvoice.InvoiceRows = order.Elements(ns + "InvoiceRows")
                                                        .Elements(ns + "InvoiceRow")
                                                        .Select(e => new Invoices.InvoiceRow

                                                            ProductId = e.Element(ns + "ProductId") == null ? 0 : (int)e.Element(ns + "ProductId"),
                                                            RowId = e.Element(ns + "RowId") == null ? 0 : (int)e.Element(ns + "RowId"),
                                                            Price = e.Element(ns + "Price") == null ? decimal.Zero : (decimal)e.Element(ns + "Price"),
                                                            Name = e.Element(ns + "Name") == null ? string.Empty : (string)e.Element(ns + "Name"),
                                                            DiscountRate = e.Element(ns + "DiscountRate") == null ? decimal.Zero : (decimal)e.Element(ns + "DiscountRate"),
                                                            Quantity = e.Element(ns + "Quantity") == null ? decimal.Zero : (decimal)e.Element(ns + "Quantity"),
                                                            Cost = e.Element(ns + "Cost") == null ? decimal.Zero : (decimal)e.Element(ns + "Cost"),
                                                            InPrice = e.Element(ns + "InPrice") == null ? decimal.Zero : (decimal)e.Element(ns + "InPrice"),
                                                            ChangeState = invState,

Open in new window

Jordan JohnsonAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Karrtik IyerSoftware ArchitectCommented:
Based on my understanding, there are multiple approaches possible,
1. One of the better one could have been to make order object serializable so that directly from XML you can serialize the order object so no need to manually parse the XML file. Also we could have done data binding to the XML as data source.
2. Second could be to just delete/reset/remove the current order object (from the collection if you are maintaining) and recreate a new one irrespective of whether the XML file is modified or newly created. Something like
if( orderidfound==true){
orderobj = new Order();
//Add the order to your collection
//After this the code follows the same path as is the case when order is not found

Open in new window

3. Third is to write an else condition to your code if(orderid==false) and in that instead of creating new invoice/invoice rows, based on elements found from XML, using some key like invoice ID, retrieve the corresponding invoice object from the invoice collection present in the already existing order object. And once that invoice row object is retrieved, update this object with the values from the XML file.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Fernando SotoRetiredCommented:
It looks like you are transforming a collection into a XML document and that you are using Linq to XML to do it. If you can show what this is defined as you can Linq in a loop to create the nodes and then attach to the main document.
Jordan JohnsonAuthor Commented:
Thanks, this helped me to resolve this. I ended up going the serialization route, as this simplified things greatly!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.