XML Literals in VB.NET Code don't seem work with #if statements











up vote
-1
down vote

favorite












I have a vb.net project that uses an XML literal to configure the user interface navigation bar. The code looks something like this and has been working for many years.



Private ReadOnly _actionTreeXml As XElement =
<nodes>
<node key="any" name="Top">
<node key="log" name="***LOGIN***" type="everybody"></node>
<node key="op" name="Home" ctrl="uiHomePage" type="mfg"></node>
<node key="barcode" name="Barcode Entry" ctrl="EditMfgEntry" type="mfg"></node>
<node key="wip" name="Work in Progress" ctrl="QueryWIP" type="mfg"></node>
<node key="readme" name="Version Info" type="everybody"></node>
</node>
</nodes>


I recently needed to have two builds of the project that differ slightly (don't want to pay for library code for many users). So I have one build where I #if out all the ui tools related to the barcode. This works great everwhere except in XML literals like this



#if USE_BAR_CODE=1
<node key="barcode" name="Barcode Entry" ctrl="EditMfgEntry" type="mfg"> </node>
#end if


If I set USE_BAR_CODE to 0 I still get the xml literal inside the #if block, but everywhere in my code where I #if'ed regular VB source the code was not compiled.



This leads me to believe that the compliation process handles xml literals BEFORE #if statements. Am I missing something?










share|improve this question




























    up vote
    -1
    down vote

    favorite












    I have a vb.net project that uses an XML literal to configure the user interface navigation bar. The code looks something like this and has been working for many years.



    Private ReadOnly _actionTreeXml As XElement =
    <nodes>
    <node key="any" name="Top">
    <node key="log" name="***LOGIN***" type="everybody"></node>
    <node key="op" name="Home" ctrl="uiHomePage" type="mfg"></node>
    <node key="barcode" name="Barcode Entry" ctrl="EditMfgEntry" type="mfg"></node>
    <node key="wip" name="Work in Progress" ctrl="QueryWIP" type="mfg"></node>
    <node key="readme" name="Version Info" type="everybody"></node>
    </node>
    </nodes>


    I recently needed to have two builds of the project that differ slightly (don't want to pay for library code for many users). So I have one build where I #if out all the ui tools related to the barcode. This works great everwhere except in XML literals like this



    #if USE_BAR_CODE=1
    <node key="barcode" name="Barcode Entry" ctrl="EditMfgEntry" type="mfg"> </node>
    #end if


    If I set USE_BAR_CODE to 0 I still get the xml literal inside the #if block, but everywhere in my code where I #if'ed regular VB source the code was not compiled.



    This leads me to believe that the compliation process handles xml literals BEFORE #if statements. Am I missing something?










    share|improve this question


























      up vote
      -1
      down vote

      favorite









      up vote
      -1
      down vote

      favorite











      I have a vb.net project that uses an XML literal to configure the user interface navigation bar. The code looks something like this and has been working for many years.



      Private ReadOnly _actionTreeXml As XElement =
      <nodes>
      <node key="any" name="Top">
      <node key="log" name="***LOGIN***" type="everybody"></node>
      <node key="op" name="Home" ctrl="uiHomePage" type="mfg"></node>
      <node key="barcode" name="Barcode Entry" ctrl="EditMfgEntry" type="mfg"></node>
      <node key="wip" name="Work in Progress" ctrl="QueryWIP" type="mfg"></node>
      <node key="readme" name="Version Info" type="everybody"></node>
      </node>
      </nodes>


      I recently needed to have two builds of the project that differ slightly (don't want to pay for library code for many users). So I have one build where I #if out all the ui tools related to the barcode. This works great everwhere except in XML literals like this



      #if USE_BAR_CODE=1
      <node key="barcode" name="Barcode Entry" ctrl="EditMfgEntry" type="mfg"> </node>
      #end if


      If I set USE_BAR_CODE to 0 I still get the xml literal inside the #if block, but everywhere in my code where I #if'ed regular VB source the code was not compiled.



      This leads me to believe that the compliation process handles xml literals BEFORE #if statements. Am I missing something?










      share|improve this question















      I have a vb.net project that uses an XML literal to configure the user interface navigation bar. The code looks something like this and has been working for many years.



      Private ReadOnly _actionTreeXml As XElement =
      <nodes>
      <node key="any" name="Top">
      <node key="log" name="***LOGIN***" type="everybody"></node>
      <node key="op" name="Home" ctrl="uiHomePage" type="mfg"></node>
      <node key="barcode" name="Barcode Entry" ctrl="EditMfgEntry" type="mfg"></node>
      <node key="wip" name="Work in Progress" ctrl="QueryWIP" type="mfg"></node>
      <node key="readme" name="Version Info" type="everybody"></node>
      </node>
      </nodes>


      I recently needed to have two builds of the project that differ slightly (don't want to pay for library code for many users). So I have one build where I #if out all the ui tools related to the barcode. This works great everwhere except in XML literals like this



      #if USE_BAR_CODE=1
      <node key="barcode" name="Barcode Entry" ctrl="EditMfgEntry" type="mfg"> </node>
      #end if


      If I set USE_BAR_CODE to 0 I still get the xml literal inside the #if block, but everywhere in my code where I #if'ed regular VB source the code was not compiled.



      This leads me to believe that the compliation process handles xml literals BEFORE #if statements. Am I missing something?







      vb.net xml-literals






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 22 at 19:04









      JohnyL

      3,1101821




      3,1101821










      asked Nov 22 at 16:00









      Hucker

      4101616




      4101616
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          May be not the solution at all, but as proverb says "A bad bush is better than the open field". 😉



          The main idea is to use processing instruction node. If element has processing instruction above it, then skip it, otherwise include it. This way you can control the "inclusiveness" of that element.



          1) First, create extension method:



          Module XmlExtentions
          <System.Runtime.CompilerServices.Extension>
          Function Filter(root_node As IEnumerable(Of XElement)) As IEnumerable(Of XElement)
          Return root_node.First().Elements().Where(
          Function(e)
          If e.PreviousNode Is Nothing Then Return True
          Return e.PreviousNode.NodeType <> Xml.XmlNodeType.ProcessingInstruction
          End Function)
          End Function
          End Module


          2. Query our XML:



          Dim _actionTreeXml As XElement =
          <nodes>
          <node key="any" name="Top">
          <node key="log" name="***LOGIN***" type="everybody"></node>
          <node key="op" name="Home" ctrl="uiHomePage" type="mfg"></node>
          <?Skip-Element?>
          <node key="barcode" name="Barcode Entry" ctrl="EditMfgEntry" type="mfg"></node>
          <node key="wip" name="Work in Progress" ctrl="QueryWIP" type="mfg"></node>
          <node key="readme" name="Version Info" type="everybody"></node>
          </node>
          </nodes>

          Dim elements = _actionTreeXml.<node>.Filter()
          '// Do something...
          elements.ToList().ForEach(Sub(e) Console.WriteLine(e))





          share|improve this answer





















          • I'm not sure that solves the problem because I can't conditionally define which the skip the node in the xml literal... However, I could setup a list of excluded nodes in code (e.g. create a list and have exclude.add("BarCode") calls), and check that list of exclusions when I build my UI. That will work. Not great because I need to have run time code to do this...a bad bush indeed.
            – Hucker
            Nov 22 at 20:04












          • @Hucker I can't conditionally define... Beg your pardon, but in your question you wanted to do this very thing...
            – JohnyL
            Nov 22 at 20:07










          • The previous answer says you can't conditionally exclude a part of the tree, only the whole thing.
            – Hucker
            Nov 22 at 20:08












          • @Hucker I guess, you have X/Y problem
            – JohnyL
            Nov 22 at 20:11










          • you can't conditionally exclude a part of the tree, very true indeed.
            – Çöđěxěŕ
            Nov 22 at 20:13


















          up vote
          1
          down vote













          Unfortunately no, you can't use directives like this in literals. Under the hood, the behavior is the same as the If, Then and Else statements. You can't put those directly in line the way you are, you have to wrap the code block to tell the compiler what to compile versus run time compile like the standard If, Then and Else statements. The statements within a conditional compilation block must be complete logical statements. For example, you cannot conditionally compile only the attributes of literals, functions etc...



          So a quick solution is below to either include the node or not include it. This was tested as well and works just fine.



          #If USE_BAR_CODE = 1 Then
          Private ReadOnly _actionTreeXml As XElement = <nodes>
          <node key="any" name="Top">
          <node key="log" name="***LOGIN***" type="everybody"></node>
          <node key="op" name="Home" ctrl="uiHomePage" type="mfg"></node>
          <node key="barcode" name="Barcode Entry" ctrl="EditMfgEntry" type="mfg"></node>
          <node key="wip" name="Work in Progress" ctrl="QueryWIP" type="mfg"></node>
          <node key="readme" name="Version Info" type="everybody"></node>
          </node>
          </nodes>
          #Else
          Private ReadOnly _actionTreeXml As XElement = <nodes>
          <node key="any" name="Top">
          <node key="log" name="***LOGIN***" type="everybody"></node>
          <node key="op" name="Home" ctrl="uiHomePage" type="mfg"></node>
          <node key="wip" name="Work in Progress" ctrl="QueryWIP" type="mfg"></node>
          <node key="readme" name="Version Info" type="everybody"></node>
          </node>
          </nodes>


          #End If





          share|improve this answer























          • Please, re-format your code...
            – JohnyL
            Nov 22 at 19:06










          • @JohnyL please explain? VS does this for me as well as my two extensions...
            – Çöđěxěŕ
            Nov 22 at 19:07












          • I don't believe it 😉 Did you try to press Enter before <nodes>? :)
            – JohnyL
            Nov 22 at 19:23










          • Depends on what version of VS a user is using, automatic code reformatting was introduced in 2015 for two steps; newline and formatting... Also ReSharper as well as CodeFormatter puts it back in-line, VS doesn't on my version.
            – Çöđěxěŕ
            Nov 22 at 19:28












          • Also using a directive like above (in the first condition), VS doesn't format the code when entered is pressed, it does indeed drop to a newline, but doesn't format the code :). Is there anything else I may be of help?
            – Çöđěxěŕ
            Nov 22 at 19:37













          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53434611%2fxml-literals-in-vb-net-code-dont-seem-work-with-if-statements%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          0
          down vote



          accepted










          May be not the solution at all, but as proverb says "A bad bush is better than the open field". 😉



          The main idea is to use processing instruction node. If element has processing instruction above it, then skip it, otherwise include it. This way you can control the "inclusiveness" of that element.



          1) First, create extension method:



          Module XmlExtentions
          <System.Runtime.CompilerServices.Extension>
          Function Filter(root_node As IEnumerable(Of XElement)) As IEnumerable(Of XElement)
          Return root_node.First().Elements().Where(
          Function(e)
          If e.PreviousNode Is Nothing Then Return True
          Return e.PreviousNode.NodeType <> Xml.XmlNodeType.ProcessingInstruction
          End Function)
          End Function
          End Module


          2. Query our XML:



          Dim _actionTreeXml As XElement =
          <nodes>
          <node key="any" name="Top">
          <node key="log" name="***LOGIN***" type="everybody"></node>
          <node key="op" name="Home" ctrl="uiHomePage" type="mfg"></node>
          <?Skip-Element?>
          <node key="barcode" name="Barcode Entry" ctrl="EditMfgEntry" type="mfg"></node>
          <node key="wip" name="Work in Progress" ctrl="QueryWIP" type="mfg"></node>
          <node key="readme" name="Version Info" type="everybody"></node>
          </node>
          </nodes>

          Dim elements = _actionTreeXml.<node>.Filter()
          '// Do something...
          elements.ToList().ForEach(Sub(e) Console.WriteLine(e))





          share|improve this answer





















          • I'm not sure that solves the problem because I can't conditionally define which the skip the node in the xml literal... However, I could setup a list of excluded nodes in code (e.g. create a list and have exclude.add("BarCode") calls), and check that list of exclusions when I build my UI. That will work. Not great because I need to have run time code to do this...a bad bush indeed.
            – Hucker
            Nov 22 at 20:04












          • @Hucker I can't conditionally define... Beg your pardon, but in your question you wanted to do this very thing...
            – JohnyL
            Nov 22 at 20:07










          • The previous answer says you can't conditionally exclude a part of the tree, only the whole thing.
            – Hucker
            Nov 22 at 20:08












          • @Hucker I guess, you have X/Y problem
            – JohnyL
            Nov 22 at 20:11










          • you can't conditionally exclude a part of the tree, very true indeed.
            – Çöđěxěŕ
            Nov 22 at 20:13















          up vote
          0
          down vote



          accepted










          May be not the solution at all, but as proverb says "A bad bush is better than the open field". 😉



          The main idea is to use processing instruction node. If element has processing instruction above it, then skip it, otherwise include it. This way you can control the "inclusiveness" of that element.



          1) First, create extension method:



          Module XmlExtentions
          <System.Runtime.CompilerServices.Extension>
          Function Filter(root_node As IEnumerable(Of XElement)) As IEnumerable(Of XElement)
          Return root_node.First().Elements().Where(
          Function(e)
          If e.PreviousNode Is Nothing Then Return True
          Return e.PreviousNode.NodeType <> Xml.XmlNodeType.ProcessingInstruction
          End Function)
          End Function
          End Module


          2. Query our XML:



          Dim _actionTreeXml As XElement =
          <nodes>
          <node key="any" name="Top">
          <node key="log" name="***LOGIN***" type="everybody"></node>
          <node key="op" name="Home" ctrl="uiHomePage" type="mfg"></node>
          <?Skip-Element?>
          <node key="barcode" name="Barcode Entry" ctrl="EditMfgEntry" type="mfg"></node>
          <node key="wip" name="Work in Progress" ctrl="QueryWIP" type="mfg"></node>
          <node key="readme" name="Version Info" type="everybody"></node>
          </node>
          </nodes>

          Dim elements = _actionTreeXml.<node>.Filter()
          '// Do something...
          elements.ToList().ForEach(Sub(e) Console.WriteLine(e))





          share|improve this answer





















          • I'm not sure that solves the problem because I can't conditionally define which the skip the node in the xml literal... However, I could setup a list of excluded nodes in code (e.g. create a list and have exclude.add("BarCode") calls), and check that list of exclusions when I build my UI. That will work. Not great because I need to have run time code to do this...a bad bush indeed.
            – Hucker
            Nov 22 at 20:04












          • @Hucker I can't conditionally define... Beg your pardon, but in your question you wanted to do this very thing...
            – JohnyL
            Nov 22 at 20:07










          • The previous answer says you can't conditionally exclude a part of the tree, only the whole thing.
            – Hucker
            Nov 22 at 20:08












          • @Hucker I guess, you have X/Y problem
            – JohnyL
            Nov 22 at 20:11










          • you can't conditionally exclude a part of the tree, very true indeed.
            – Çöđěxěŕ
            Nov 22 at 20:13













          up vote
          0
          down vote



          accepted







          up vote
          0
          down vote



          accepted






          May be not the solution at all, but as proverb says "A bad bush is better than the open field". 😉



          The main idea is to use processing instruction node. If element has processing instruction above it, then skip it, otherwise include it. This way you can control the "inclusiveness" of that element.



          1) First, create extension method:



          Module XmlExtentions
          <System.Runtime.CompilerServices.Extension>
          Function Filter(root_node As IEnumerable(Of XElement)) As IEnumerable(Of XElement)
          Return root_node.First().Elements().Where(
          Function(e)
          If e.PreviousNode Is Nothing Then Return True
          Return e.PreviousNode.NodeType <> Xml.XmlNodeType.ProcessingInstruction
          End Function)
          End Function
          End Module


          2. Query our XML:



          Dim _actionTreeXml As XElement =
          <nodes>
          <node key="any" name="Top">
          <node key="log" name="***LOGIN***" type="everybody"></node>
          <node key="op" name="Home" ctrl="uiHomePage" type="mfg"></node>
          <?Skip-Element?>
          <node key="barcode" name="Barcode Entry" ctrl="EditMfgEntry" type="mfg"></node>
          <node key="wip" name="Work in Progress" ctrl="QueryWIP" type="mfg"></node>
          <node key="readme" name="Version Info" type="everybody"></node>
          </node>
          </nodes>

          Dim elements = _actionTreeXml.<node>.Filter()
          '// Do something...
          elements.ToList().ForEach(Sub(e) Console.WriteLine(e))





          share|improve this answer












          May be not the solution at all, but as proverb says "A bad bush is better than the open field". 😉



          The main idea is to use processing instruction node. If element has processing instruction above it, then skip it, otherwise include it. This way you can control the "inclusiveness" of that element.



          1) First, create extension method:



          Module XmlExtentions
          <System.Runtime.CompilerServices.Extension>
          Function Filter(root_node As IEnumerable(Of XElement)) As IEnumerable(Of XElement)
          Return root_node.First().Elements().Where(
          Function(e)
          If e.PreviousNode Is Nothing Then Return True
          Return e.PreviousNode.NodeType <> Xml.XmlNodeType.ProcessingInstruction
          End Function)
          End Function
          End Module


          2. Query our XML:



          Dim _actionTreeXml As XElement =
          <nodes>
          <node key="any" name="Top">
          <node key="log" name="***LOGIN***" type="everybody"></node>
          <node key="op" name="Home" ctrl="uiHomePage" type="mfg"></node>
          <?Skip-Element?>
          <node key="barcode" name="Barcode Entry" ctrl="EditMfgEntry" type="mfg"></node>
          <node key="wip" name="Work in Progress" ctrl="QueryWIP" type="mfg"></node>
          <node key="readme" name="Version Info" type="everybody"></node>
          </node>
          </nodes>

          Dim elements = _actionTreeXml.<node>.Filter()
          '// Do something...
          elements.ToList().ForEach(Sub(e) Console.WriteLine(e))






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 22 at 19:33









          JohnyL

          3,1101821




          3,1101821












          • I'm not sure that solves the problem because I can't conditionally define which the skip the node in the xml literal... However, I could setup a list of excluded nodes in code (e.g. create a list and have exclude.add("BarCode") calls), and check that list of exclusions when I build my UI. That will work. Not great because I need to have run time code to do this...a bad bush indeed.
            – Hucker
            Nov 22 at 20:04












          • @Hucker I can't conditionally define... Beg your pardon, but in your question you wanted to do this very thing...
            – JohnyL
            Nov 22 at 20:07










          • The previous answer says you can't conditionally exclude a part of the tree, only the whole thing.
            – Hucker
            Nov 22 at 20:08












          • @Hucker I guess, you have X/Y problem
            – JohnyL
            Nov 22 at 20:11










          • you can't conditionally exclude a part of the tree, very true indeed.
            – Çöđěxěŕ
            Nov 22 at 20:13


















          • I'm not sure that solves the problem because I can't conditionally define which the skip the node in the xml literal... However, I could setup a list of excluded nodes in code (e.g. create a list and have exclude.add("BarCode") calls), and check that list of exclusions when I build my UI. That will work. Not great because I need to have run time code to do this...a bad bush indeed.
            – Hucker
            Nov 22 at 20:04












          • @Hucker I can't conditionally define... Beg your pardon, but in your question you wanted to do this very thing...
            – JohnyL
            Nov 22 at 20:07










          • The previous answer says you can't conditionally exclude a part of the tree, only the whole thing.
            – Hucker
            Nov 22 at 20:08












          • @Hucker I guess, you have X/Y problem
            – JohnyL
            Nov 22 at 20:11










          • you can't conditionally exclude a part of the tree, very true indeed.
            – Çöđěxěŕ
            Nov 22 at 20:13
















          I'm not sure that solves the problem because I can't conditionally define which the skip the node in the xml literal... However, I could setup a list of excluded nodes in code (e.g. create a list and have exclude.add("BarCode") calls), and check that list of exclusions when I build my UI. That will work. Not great because I need to have run time code to do this...a bad bush indeed.
          – Hucker
          Nov 22 at 20:04






          I'm not sure that solves the problem because I can't conditionally define which the skip the node in the xml literal... However, I could setup a list of excluded nodes in code (e.g. create a list and have exclude.add("BarCode") calls), and check that list of exclusions when I build my UI. That will work. Not great because I need to have run time code to do this...a bad bush indeed.
          – Hucker
          Nov 22 at 20:04














          @Hucker I can't conditionally define... Beg your pardon, but in your question you wanted to do this very thing...
          – JohnyL
          Nov 22 at 20:07




          @Hucker I can't conditionally define... Beg your pardon, but in your question you wanted to do this very thing...
          – JohnyL
          Nov 22 at 20:07












          The previous answer says you can't conditionally exclude a part of the tree, only the whole thing.
          – Hucker
          Nov 22 at 20:08






          The previous answer says you can't conditionally exclude a part of the tree, only the whole thing.
          – Hucker
          Nov 22 at 20:08














          @Hucker I guess, you have X/Y problem
          – JohnyL
          Nov 22 at 20:11




          @Hucker I guess, you have X/Y problem
          – JohnyL
          Nov 22 at 20:11












          you can't conditionally exclude a part of the tree, very true indeed.
          – Çöđěxěŕ
          Nov 22 at 20:13




          you can't conditionally exclude a part of the tree, very true indeed.
          – Çöđěxěŕ
          Nov 22 at 20:13












          up vote
          1
          down vote













          Unfortunately no, you can't use directives like this in literals. Under the hood, the behavior is the same as the If, Then and Else statements. You can't put those directly in line the way you are, you have to wrap the code block to tell the compiler what to compile versus run time compile like the standard If, Then and Else statements. The statements within a conditional compilation block must be complete logical statements. For example, you cannot conditionally compile only the attributes of literals, functions etc...



          So a quick solution is below to either include the node or not include it. This was tested as well and works just fine.



          #If USE_BAR_CODE = 1 Then
          Private ReadOnly _actionTreeXml As XElement = <nodes>
          <node key="any" name="Top">
          <node key="log" name="***LOGIN***" type="everybody"></node>
          <node key="op" name="Home" ctrl="uiHomePage" type="mfg"></node>
          <node key="barcode" name="Barcode Entry" ctrl="EditMfgEntry" type="mfg"></node>
          <node key="wip" name="Work in Progress" ctrl="QueryWIP" type="mfg"></node>
          <node key="readme" name="Version Info" type="everybody"></node>
          </node>
          </nodes>
          #Else
          Private ReadOnly _actionTreeXml As XElement = <nodes>
          <node key="any" name="Top">
          <node key="log" name="***LOGIN***" type="everybody"></node>
          <node key="op" name="Home" ctrl="uiHomePage" type="mfg"></node>
          <node key="wip" name="Work in Progress" ctrl="QueryWIP" type="mfg"></node>
          <node key="readme" name="Version Info" type="everybody"></node>
          </node>
          </nodes>


          #End If





          share|improve this answer























          • Please, re-format your code...
            – JohnyL
            Nov 22 at 19:06










          • @JohnyL please explain? VS does this for me as well as my two extensions...
            – Çöđěxěŕ
            Nov 22 at 19:07












          • I don't believe it 😉 Did you try to press Enter before <nodes>? :)
            – JohnyL
            Nov 22 at 19:23










          • Depends on what version of VS a user is using, automatic code reformatting was introduced in 2015 for two steps; newline and formatting... Also ReSharper as well as CodeFormatter puts it back in-line, VS doesn't on my version.
            – Çöđěxěŕ
            Nov 22 at 19:28












          • Also using a directive like above (in the first condition), VS doesn't format the code when entered is pressed, it does indeed drop to a newline, but doesn't format the code :). Is there anything else I may be of help?
            – Çöđěxěŕ
            Nov 22 at 19:37

















          up vote
          1
          down vote













          Unfortunately no, you can't use directives like this in literals. Under the hood, the behavior is the same as the If, Then and Else statements. You can't put those directly in line the way you are, you have to wrap the code block to tell the compiler what to compile versus run time compile like the standard If, Then and Else statements. The statements within a conditional compilation block must be complete logical statements. For example, you cannot conditionally compile only the attributes of literals, functions etc...



          So a quick solution is below to either include the node or not include it. This was tested as well and works just fine.



          #If USE_BAR_CODE = 1 Then
          Private ReadOnly _actionTreeXml As XElement = <nodes>
          <node key="any" name="Top">
          <node key="log" name="***LOGIN***" type="everybody"></node>
          <node key="op" name="Home" ctrl="uiHomePage" type="mfg"></node>
          <node key="barcode" name="Barcode Entry" ctrl="EditMfgEntry" type="mfg"></node>
          <node key="wip" name="Work in Progress" ctrl="QueryWIP" type="mfg"></node>
          <node key="readme" name="Version Info" type="everybody"></node>
          </node>
          </nodes>
          #Else
          Private ReadOnly _actionTreeXml As XElement = <nodes>
          <node key="any" name="Top">
          <node key="log" name="***LOGIN***" type="everybody"></node>
          <node key="op" name="Home" ctrl="uiHomePage" type="mfg"></node>
          <node key="wip" name="Work in Progress" ctrl="QueryWIP" type="mfg"></node>
          <node key="readme" name="Version Info" type="everybody"></node>
          </node>
          </nodes>


          #End If





          share|improve this answer























          • Please, re-format your code...
            – JohnyL
            Nov 22 at 19:06










          • @JohnyL please explain? VS does this for me as well as my two extensions...
            – Çöđěxěŕ
            Nov 22 at 19:07












          • I don't believe it 😉 Did you try to press Enter before <nodes>? :)
            – JohnyL
            Nov 22 at 19:23










          • Depends on what version of VS a user is using, automatic code reformatting was introduced in 2015 for two steps; newline and formatting... Also ReSharper as well as CodeFormatter puts it back in-line, VS doesn't on my version.
            – Çöđěxěŕ
            Nov 22 at 19:28












          • Also using a directive like above (in the first condition), VS doesn't format the code when entered is pressed, it does indeed drop to a newline, but doesn't format the code :). Is there anything else I may be of help?
            – Çöđěxěŕ
            Nov 22 at 19:37















          up vote
          1
          down vote










          up vote
          1
          down vote









          Unfortunately no, you can't use directives like this in literals. Under the hood, the behavior is the same as the If, Then and Else statements. You can't put those directly in line the way you are, you have to wrap the code block to tell the compiler what to compile versus run time compile like the standard If, Then and Else statements. The statements within a conditional compilation block must be complete logical statements. For example, you cannot conditionally compile only the attributes of literals, functions etc...



          So a quick solution is below to either include the node or not include it. This was tested as well and works just fine.



          #If USE_BAR_CODE = 1 Then
          Private ReadOnly _actionTreeXml As XElement = <nodes>
          <node key="any" name="Top">
          <node key="log" name="***LOGIN***" type="everybody"></node>
          <node key="op" name="Home" ctrl="uiHomePage" type="mfg"></node>
          <node key="barcode" name="Barcode Entry" ctrl="EditMfgEntry" type="mfg"></node>
          <node key="wip" name="Work in Progress" ctrl="QueryWIP" type="mfg"></node>
          <node key="readme" name="Version Info" type="everybody"></node>
          </node>
          </nodes>
          #Else
          Private ReadOnly _actionTreeXml As XElement = <nodes>
          <node key="any" name="Top">
          <node key="log" name="***LOGIN***" type="everybody"></node>
          <node key="op" name="Home" ctrl="uiHomePage" type="mfg"></node>
          <node key="wip" name="Work in Progress" ctrl="QueryWIP" type="mfg"></node>
          <node key="readme" name="Version Info" type="everybody"></node>
          </node>
          </nodes>


          #End If





          share|improve this answer














          Unfortunately no, you can't use directives like this in literals. Under the hood, the behavior is the same as the If, Then and Else statements. You can't put those directly in line the way you are, you have to wrap the code block to tell the compiler what to compile versus run time compile like the standard If, Then and Else statements. The statements within a conditional compilation block must be complete logical statements. For example, you cannot conditionally compile only the attributes of literals, functions etc...



          So a quick solution is below to either include the node or not include it. This was tested as well and works just fine.



          #If USE_BAR_CODE = 1 Then
          Private ReadOnly _actionTreeXml As XElement = <nodes>
          <node key="any" name="Top">
          <node key="log" name="***LOGIN***" type="everybody"></node>
          <node key="op" name="Home" ctrl="uiHomePage" type="mfg"></node>
          <node key="barcode" name="Barcode Entry" ctrl="EditMfgEntry" type="mfg"></node>
          <node key="wip" name="Work in Progress" ctrl="QueryWIP" type="mfg"></node>
          <node key="readme" name="Version Info" type="everybody"></node>
          </node>
          </nodes>
          #Else
          Private ReadOnly _actionTreeXml As XElement = <nodes>
          <node key="any" name="Top">
          <node key="log" name="***LOGIN***" type="everybody"></node>
          <node key="op" name="Home" ctrl="uiHomePage" type="mfg"></node>
          <node key="wip" name="Work in Progress" ctrl="QueryWIP" type="mfg"></node>
          <node key="readme" name="Version Info" type="everybody"></node>
          </node>
          </nodes>


          #End If






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 22 at 19:42

























          answered Nov 22 at 18:58









          Çöđěxěŕ

          5,14051740




          5,14051740












          • Please, re-format your code...
            – JohnyL
            Nov 22 at 19:06










          • @JohnyL please explain? VS does this for me as well as my two extensions...
            – Çöđěxěŕ
            Nov 22 at 19:07












          • I don't believe it 😉 Did you try to press Enter before <nodes>? :)
            – JohnyL
            Nov 22 at 19:23










          • Depends on what version of VS a user is using, automatic code reformatting was introduced in 2015 for two steps; newline and formatting... Also ReSharper as well as CodeFormatter puts it back in-line, VS doesn't on my version.
            – Çöđěxěŕ
            Nov 22 at 19:28












          • Also using a directive like above (in the first condition), VS doesn't format the code when entered is pressed, it does indeed drop to a newline, but doesn't format the code :). Is there anything else I may be of help?
            – Çöđěxěŕ
            Nov 22 at 19:37




















          • Please, re-format your code...
            – JohnyL
            Nov 22 at 19:06










          • @JohnyL please explain? VS does this for me as well as my two extensions...
            – Çöđěxěŕ
            Nov 22 at 19:07












          • I don't believe it 😉 Did you try to press Enter before <nodes>? :)
            – JohnyL
            Nov 22 at 19:23










          • Depends on what version of VS a user is using, automatic code reformatting was introduced in 2015 for two steps; newline and formatting... Also ReSharper as well as CodeFormatter puts it back in-line, VS doesn't on my version.
            – Çöđěxěŕ
            Nov 22 at 19:28












          • Also using a directive like above (in the first condition), VS doesn't format the code when entered is pressed, it does indeed drop to a newline, but doesn't format the code :). Is there anything else I may be of help?
            – Çöđěxěŕ
            Nov 22 at 19:37


















          Please, re-format your code...
          – JohnyL
          Nov 22 at 19:06




          Please, re-format your code...
          – JohnyL
          Nov 22 at 19:06












          @JohnyL please explain? VS does this for me as well as my two extensions...
          – Çöđěxěŕ
          Nov 22 at 19:07






          @JohnyL please explain? VS does this for me as well as my two extensions...
          – Çöđěxěŕ
          Nov 22 at 19:07














          I don't believe it 😉 Did you try to press Enter before <nodes>? :)
          – JohnyL
          Nov 22 at 19:23




          I don't believe it 😉 Did you try to press Enter before <nodes>? :)
          – JohnyL
          Nov 22 at 19:23












          Depends on what version of VS a user is using, automatic code reformatting was introduced in 2015 for two steps; newline and formatting... Also ReSharper as well as CodeFormatter puts it back in-line, VS doesn't on my version.
          – Çöđěxěŕ
          Nov 22 at 19:28






          Depends on what version of VS a user is using, automatic code reformatting was introduced in 2015 for two steps; newline and formatting... Also ReSharper as well as CodeFormatter puts it back in-line, VS doesn't on my version.
          – Çöđěxěŕ
          Nov 22 at 19:28














          Also using a directive like above (in the first condition), VS doesn't format the code when entered is pressed, it does indeed drop to a newline, but doesn't format the code :). Is there anything else I may be of help?
          – Çöđěxěŕ
          Nov 22 at 19:37






          Also using a directive like above (in the first condition), VS doesn't format the code when entered is pressed, it does indeed drop to a newline, but doesn't format the code :). Is there anything else I may be of help?
          – Çöđěxěŕ
          Nov 22 at 19:37




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53434611%2fxml-literals-in-vb-net-code-dont-seem-work-with-if-statements%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          What visual should I use to simply compare current year value vs last year in Power BI desktop

          Alexandru Averescu

          Trompette piccolo