<p>I'm building a listing/grid control in a <code>Flex</code> application and using it in a <code>.NET</code> web application.  To make a really long story short I am getting XML from a webservice of serialized objects.  I have a page limit of how many things can be on a page.  I've taken a data grid and made it page, sort across pages, and handle some basic filtering.  </p>

<p>In regards to paging I'm using a Dictionary keyed on the page and storing the XML for that page.  This way whenever a user comes back to a page that I've saved into this dictionary I can grab the XML from local memory instead of hitting the webservice.  Basically, I'm caching the data retrieved from each call to the webservice for a page of data.</p>

<p>There are several things that can expire my cache.  Filtering and sorting are the main reason.  However, a user may edit a row of data in the grid by opening an editor.  The data they edit could cause the data displayed in the row to be stale.  I could easily go to the webservice and get the whole page of data, but since the page size is set at runtime I could be looking at a large amount of records to retrieve.</p>

<p>So let me now get to the heart of the issue that I am experiencing.  In order to prevent getting the whole page of data back I make a call to the webservice asking for the completely updated record (the editor handles saving its data).</p>

<p>Since I'm using custom objects I need to serialize them on the server to XML (this is handled already for other portions of our software).  All data is handled through XML in e4x.  The cache in the Dictionary is stored as an XMLList.</p>

<p><strong>Now let me show you my code...</strong></p>

<pre><code>var idOfReplacee:String = this._WebService.GetSingleModelXml.lastResult.*[0].*[0].@Id;
var xmlToReplace:XMLList = this._DataPages[this._Options.PageIndex].Data.(@Id == idOfReplacee);

if(xmlToReplace.length() &gt; 0)
{
    delete (this._DataPages[this._Options.PageIndex].Data.(@Id == idOfReplacee)[0]);
    this._DataPages[this._Options.PageIndex].Data += this._WebService.GetSingleModelXml.lastResult.*[0].*[0];
}
</code></pre>

<p>Basically, I get the id of the node I want to replace.  Then I find it in the cache's Data property (<code>XMLList</code>).  I make sure it exists since the filter on the second line returns the <code>XMLList</code>.</p>

<p>The problem I have is with the delete line.  I cannot make that line delete that node from the list.  The line following the delete line works.  I've added the node to the list.</p>

<p>How do I replace or delete that node (meaning the node that I find from the filter statement out of the .Data property of the cache)???</p>

<p>Hopefully the underscores for all of my variables do not stay escaped when this is posted! otherwise <code>this.&amp;#95 == this</code>._</p>

[{'answer_id': 11048, 'author': 'Matt Dillard', 'author_id': 863, 'author_profile': 'https://Stackoverflow.com/users/863', 'pm_score': 0, 'selected': False, 'text': '<p>I don\'t immediately see the problem, so I can only venture a guess.  The <code>delete</code> line that you\'ve got is looking for the first item <em>at the top level</em> of the list which has an attribute "Id" with a value equal to <code>idOfReplacee</code>.  Ensure that you don\'t need to dig deeper into the XML structure to find that matching id.</p>\n\n<p>Try this instead:</p>\n\n<pre><code>delete (this._DataPages[this._Options.PageIndex].Data..(@Id == idOfReplacee)[0]);\n</code></pre>\n\n<p>(Notice the extra \'.\' after <code>Data</code>).  You could more easily debug this by setting a breakpoint on the second line of the code you posted, and ensure that the XMLList looks like you expect.</p>\n'}, {'answer_id': 11059, 'author': 'Theo', 'author_id': 1109, 'author_profile': 'https://Stackoverflow.com/users/1109', 'pm_score': 1, 'selected': False, 'text': '<p>Perhaps you could use <code>replace</code> instead?</p>\n\n<pre><code>var oldNode : XML = this._DataPages[this._Options.PageIndex].Data.(@Id == idOfReplacee)[0];\nvar newNode : XML = this._WebService.GetSingleModelXml.lastResult.*[0].*[0];    \n\noldNode.parent.replace(oldNode, newNode);\n</code></pre>\n'}, {'answer_id': 11527, 'author': 'Mike G', 'author_id': 1290, 'author_profile': 'https://Stackoverflow.com/users/1290', 'pm_score': 2, 'selected': False, 'text': '<p>Thanks for the answers guys.</p>\n\n<p>@Theo:\nI tried the replace several different ways.  For some reason it would never error, but never update the list.</p>\n\n<p>@Matt:\nI figured out a solution.  The issue wasn\'t coming from what you suggested, but from how the delete works with Lists (at least how I have it in this instance).</p>\n\n<p>The Data property of the _DataPages dictionary object is list of the definition nodes (was arrived at by a previous filtering of another XML document).</p>\n\n<pre><code>&lt;Models&gt;\n     &lt;Definition Id=\'1\' /&gt;\n     &lt;Definition Id=\'2\' /&gt;\n&lt;/Models&gt;\n</code></pre>\n\n<p>I ended up doing this little deal:</p>\n\n<pre><code>//gets the index of the node to replace from the same filter\nvar childIndex:int = (this._DataPages[this._Options.PageIndex].Data.(@Id == idOfReplacee)[0]).childIndex();\n//deletes the node from the list\ndelete this._DataPages[this._Options.PageIndex].Data[childIndex];\n//appends the new node from the webservice to the list\nthis._DataPages[this._Options.PageIndex].Data += this._WebService.GetSingleModelXml.lastResult.*[0].*[0];\n</code></pre>\n\n<p>So basically I had to get the index of the node in the XMLList that is the Data property.  From there I could use the delete keyword to remove it from the list.  The += adds my new node to the list.</p>\n\n<p>I\'m so used to using the ActiveX or Mozilla XmlDocument stuff where you call "SelectSingleNode" and then use "replaceChild" to do this kind of stuff.  Oh well, at least this is in some forum where someone else can find it.  I do not know the procedure for what happens when I answer my own question.  Perhaps this insight will help someone else come along and help answer the question better!</p>\n'}, {'answer_id': 37029264, 'author': 'Harbs', 'author_id': 5475183, 'author_profile': 'https://Stackoverflow.com/users/5475183', 'pm_score': 1, 'selected': False, 'text': "<p>I know this is an incredibly old question, but I don't see (what I think is) the simplest solution to this problem.</p>\n\n<p>Theo had the right direction here, but there's a number of errors with the way replace was being used (and the fact that pretty much everything in E4X is a function).</p>\n\n<p>I believe this will do the trick:</p>\n\n<pre><code>oldNode.parent().replace(oldNode.childIndex(), newNode);\n</code></pre>\n\n<p><code>replace()</code> can take a number of different types in the first parameter, but AFAIK, XML objects are not one of them.</p>\n"}]
['https://Stackoverflow.com/questions/10926', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1290/']