We help IT Professionals succeed at work.
Get Started

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

Jordan Johnson
Last Modified: 2015-12-01
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="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<OrderId xmlns="http://24sevenOffice.com/webservices">88844165</OrderId>

<CustomerId xmlns="http://24sevenOffice.com/webservices">7</CustomerId>

<CustomerName xmlns="http://24sevenOffice.com/webservices">Test Company</CustomerName>

-<Addresses xmlns="http://24sevenOffice.com/webservices">


<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="http://24sevenOffice.com/webservices">Invoiced</OrderStatus>

<DateOrdered xmlns="http://24sevenOffice.com/webservices">2015-11-02</DateOrdered>


<IncludeVAT xmlns="http://24sevenOffice.com/webservices">True</IncludeVAT>

-<Currency xmlns="http://24sevenOffice.com/webservices">



<TypeOfSaleId xmlns="http://24sevenOffice.com/webservices">-100</TypeOfSaleId>

-<InvoiceRows xmlns="http://24sevenOffice.com/webservices">





<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

Watch Question
Software Architect
This problem has been solved!
Unlock 1 Answer and 3 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE