score:0

if it really takes this long to run this, then maybe do something like this:

  1. don't iterate both - only iterate the xml-file and load the data from your datalst (make a sql-command or simple linq-statement to load the data based on name/store/section), make a simple struct/class for your key with this data (name/store/section) - don't forget to implement equals, and gethashcode
  2. iterate through your xml-elements and use the dictionary to find the values to replace

this way you will only iterate the xml-file once not once for every data in your datasource.

score:0

it's not clear why it's taking that long - that's a very long time. how many elements are in datalst? i would rewrite the query for simplicity to start with though:

ienumerable<xelement> elements = xmldoc.descendants();
foreach (datasource data in datalst)
{ 
    xelement valelement = (from element in xmldoc.descendants(data.name)
                           where data.store == element.element("store").value
                              && data.section == element.element("section").value
                           select element.element("val")).single();

    valelement.replacewith(new xelement("val"), data.value));
} 

(i'm assuming none of your elements actually have namespaces, by the way.)

next up: consider replacing the contents of valelement instead of replacing the element itself. change it to:

valelement.replaceall(data.value);

now, this has all been trying to keep to the simplicity of avoiding precomputation etc... because it sounds like it shouldn't take this long. however, you may need to build lookups as marc and carsten suggested.

score:0

try by replacing single() call in the linq with first().

score:0

at the risk of flaming, have you considered writing this in xquery instead? there's a good chance that a decent xquery processor would have a join optimiser that handles this query efficiently.

score:0

"well thanks everyone for your precious time and effort"

problem answer: actually the object 'datalst' was of the type ienumerable<> which was taking time in obtaining the values but after i changed it to the list<> type the performance improved drastically (now running in 20 seconds)

score:1

i think better approach would be to deserialize xml to c# classes and then use linq on that, should be fast.

score:2

it looks like you have an o(n)×o(m) issue here, for n = size of datalist and m = size of the xml. to make this o(n)+o(m), you should index the data; for example:

var lookup = elements.tolookup(
       x => new {
            name = x.name.localname,
            store = x.element(xname.get("store", "")).value,
            section =  x.element(xname.get("section", "")).value},
       x =>  x.element(xname.get("val", ""))
    );

foreach (datasource data in datalst)
{ 
    xelement xmlelem = lookup[
          new {data.name, data.store, data.section}].single();
    xmlelem.replacewith(new xelement(xname.get("val", ""), data.value));
}

(untested - to show general approach only)


Related Query

More Query from same tag