{"id":4413,"date":"2008-05-08T12:47:00","date_gmt":"2008-05-08T12:47:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/vbteam\/2008\/05\/08\/vb-xml-cookbook-recipe-5-the-halloween-problem-doug-rothaus\/"},"modified":"2024-07-05T14:25:33","modified_gmt":"2024-07-05T21:25:33","slug":"vb-xml-cookbook-recipe-5-the-halloween-problem-doug-rothaus","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/vbteam\/vb-xml-cookbook-recipe-5-the-halloween-problem-doug-rothaus\/","title":{"rendered":"VB XML Cookbook, Recipe 5: The &#8220;Halloween&#8221; Problem (Doug Rothaus)"},"content":{"rendered":"<p><font face=\"Calibri\"><\/p>\n<p class=\"MsoNormal\"><font size=\"3\">In the last two XML cookbook entries, we talked about the technique of using the <\/font><a href=\"http:\/\/msdn2.microsoft.com\/en-us\/library\/system.xml.linq.xnode.replacewith.aspx\"><font color=\"#0000ff\" size=\"3\">ReplaceWith<\/font><\/a><font size=\"3\"> method to perform an identity transform. While this technique may meet your needs, it can introduce a problem in your code commonly referred to as the \u201cHalloween\u201d problem. Let\u2019s take a look at what the problem is, and how to solve it. (For details on the \u201cHalloween\u201d problem and recommended solutions, see <\/font><a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb387088.aspx\"><font size=\"3\">this topic in the documentation<\/font><\/a><font size=\"3\">.)<\/font><\/p>\n<p class=\"MsoNormal\"><font size=\"3\">The \u201cHalloween\u201d problem describes a scenario where you have a set of data that is updated in some fashion while enumerating through that data set. As a result, you can encounter a null reference exception, or worse, you can end up modifying the wrong data. For example, consider this snippet of code based on the previous two XML cookbook recipe posts:<\/font><\/font><span>&nbsp;<\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp; <\/span><span>Private<\/span> <span>Sub<\/span> Recipe5(<span>ByVal<\/span> xmlPath <span>As<\/span> <span>String<\/span>)<\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp; <\/span><span>Dim<\/span> xmlDoc = XDocument.Load(xmlPath)<\/span><\/p>\n<p class=\"MsoNormal\"><span>&nbsp;<\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp; <\/span><span>Dim<\/span> info = xmlDoc.<span>&lt;<\/span>Contacts<span>&gt;<\/span>.<span>&lt;<\/span>Contact<span>&gt;<\/span>.<span>&lt;<\/span>aci:AdditionalContactInfo<span>&gt;<\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span>&nbsp;<\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp; <\/span><span>&#8216; Replace e-mail address tags with mailto links.<\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp; <\/span><span>For<\/span> <span>Each<\/span> email <span>In<\/span> info&#8230;<span>&lt;<\/span>act:eMail<span>&gt;<\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>TransformEmail(email)<\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp; <\/span><span>Next<\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp; <\/span><span>End<\/span> <span>Sub<\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span>&nbsp;<\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp; <\/span><span>Private<\/span> <span>Sub<\/span> TransformEmail(<span>ByVal<\/span> email <span>As<\/span> XElement)<\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp; <\/span><span>Dim<\/span> emailHtml = <span>&lt;<\/span><span>div<\/span> <span>class<\/span><span>=<\/span><span>&#8220;<\/span><span>Email<\/span><span>&#8220;<\/span><span>&gt;<\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>&lt;<\/span><span>a<\/span> <span>href<\/span><span>=<\/span><span>&lt;%=<\/span> <span>&#8220;mailto:&#8221;<\/span> &amp; email.<span>&lt;<\/span>act:eMailAddress<span>&gt;<\/span>.Value <span>%&gt;<\/span><span>&gt;<\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>&lt;%=<\/span> email.<span>&lt;<\/span>act:eMailAddress<span>&gt;<\/span>.Value <span>%&gt;<\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>&lt;\/<\/span><span>a<\/span><span>&gt;<\/span><span>&#032;<\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>&lt;\/<\/span><span>div<\/span><span>&gt;<\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span>&nbsp;<\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span>email.ReplaceWith(emailHtml)<\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp; <\/span><span>End<\/span> <span>Sub<\/span><\/span><\/p>\n<p class=\"MsoNormal\"><font face=\"Calibri\" size=\"3\">If you ran this code, you would encounter a null reference error as shown here:<\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Calibri\" size=\"3\"><img decoding=\"async\" title=\"Null Reference Exception\" height=\"237\" alt=\"Null Reference Exception\" src=\"https:\/\/devblogs.microsoft.com\/vbteam\/wp-content\/uploads\/sites\/7\/2008\/05\/NullReferenceException.jpg\" width=\"452\"><\/font><\/p>\n<p class=\"MsoNormal\"><span><\/span><\/p>\n<p class=\"MsoNormal\"><font face=\"Calibri\" size=\"3\">The reason for the error is that the <b>For\u2026Each<\/b> loop is looping through the results of a query for <\/font><span>&lt;eMail&gt;<\/span><font face=\"Calibri\" size=\"3\"> elements. During the first call to the <b>TransformEmail<\/b> function, the <\/font><span>&lt;eMail&gt;<\/span><font face=\"Calibri\" size=\"3\"> element is replaced with HTML. As a result, the query references an XML element that no longer exists.<\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Calibri\" size=\"3\">How do you solve the problem then? There are a couple of solutions. The first is what you may have noticed in the previous cookbook entries: return the query results as a <b>List<\/b> using the <b>ToList<\/b> method. That is, the following code:<\/font><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp; <\/span><span>For<\/span> <span>Each<\/span> email <span>In<\/span> info&#8230;<span>&lt;<\/span>act:eMail<span>&gt;<\/p>\n<p><\/span><\/span><\/p>\n<p class=\"MsoNormal\"><font face=\"Calibri\" size=\"3\">Is changed to\u2026<\/font><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp; <\/span><span>For<\/span> <span>Each<\/span> email <span>In<\/span> info&#8230;<span>&lt;<\/span>act:eMail<span>&gt;<\/span>.ToList()<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><font face=\"Calibri\" size=\"3\">Returning a <b>List<\/b> means that we\u2019re no longer dealing with a query. Hence, an update that would affect the query, such as replacing an XML element with HTML, does not affect the <b>List<\/b>. <\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Calibri\" size=\"3\">The other common solution to the \u201cHalloween\u201d problem is to write your code so that it does not modify the original document. Instead, you project your results into a new XML document. In the next cookbook post, we\u2019ll look at an example of how to do this.<\/font><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the last two XML cookbook entries, we talked about the technique of using the ReplaceWith method to perform an identity transform. While this technique may meet your needs, it can introduce a problem in your code commonly referred to as the \u201cHalloween\u201d problem. Let\u2019s take a look at what the problem is, and how [&hellip;]<\/p>\n","protected":false},"author":260,"featured_media":8818,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[192,195],"tags":[59,117,163,166,185],"class_list":["post-4413","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-featured","category-visual-basic","tag-doug-rothaus","tag-orcas","tag-vb-xml-cookbook","tag-vb2008","tag-xml"],"acf":[],"blog_post_summary":"<p>In the last two XML cookbook entries, we talked about the technique of using the ReplaceWith method to perform an identity transform. While this technique may meet your needs, it can introduce a problem in your code commonly referred to as the \u201cHalloween\u201d problem. Let\u2019s take a look at what the problem is, and how [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/posts\/4413","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/users\/260"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/comments?post=4413"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/posts\/4413\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/media\/8818"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/media?parent=4413"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/categories?post=4413"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/tags?post=4413"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}