Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Brian Olsen 143 posts 424 karma points
    Aug 07, 2013 @ 14:54
    Brian Olsen
    0

    Razor check whether item is empty or not in xml

    How do I make a check whether xml is empty or not

    --- XML if not empty ---

            <linkBox>

              <widgets>

                <widget isCollapsed="false">

                  <linkstype>interntlink</linkstype>

                  <boxlinktekst>test af internt link</boxlinktekst>

                  <eksternelinks />

                  <interntlink>

                    <contentPicker>

                      <content>1320</content>

                    </contentPicker>

                  </interntlink>

                </widget>

                <widget isCollapsed="false">

                  <linkstype>eksternelinks</linkstype>

                  <boxlinktekst>test af eksterne link</boxlinktekst>

                  <eksternelinks>http://www.onkelb.dk</eksternelinks>;

                  <interntlink>

                    <contentPicker />

                  </interntlink>

                </widget>

              </widgets>

            </linkBox>

     

    --- If empty -----

            <linkBox>

              <widgets>

                <widget isCollapsed="false">

                  <linkstype />

                  <boxlinktekst />

                  <eksternelinks />

                  <interntlink>

                    <contentPicker />

                  </interntlink>

                </widget>

              </widgets>

            </linkBox>

     

     

  • Amir Khan 1289 posts 2746 karma points
    Aug 07, 2013 @ 16:42
    Amir Khan
    0

    Hi Brian, something like this perhaps? Do you need help reading the RSS feed in general or just checking for the value?

    @if(@node.Attributes["linkstype"].InnerText.ToString() == null)
    {
    do stuff
    }

     

  • Psib3r 70 posts 119 karma points
    Aug 07, 2013 @ 17:03
    Psib3r
    0
    XElement root1 = XElement.Parse(
    @"<linkBox>
        <widgets>
          <widget isCollapsed='false'>
            <linkstype />
            <boxlinktekst />
            <eksternelinks />
            <interntlink>
              <contentPicker />
            </interntlink>
          </widget>
        </widgets>
      </linkBox>");
    
    var links1 = from x in root1.Elements("widgets").Elements("widget")
                 where x.Element("eksternelinks").Value != "" || x.Element("interntlink").Element("contentPicker").Descendants("content").Count() > 0
    
                 select x;
    
    
    XElement root2 = XElement.Parse(
      @"<linkBox>
          <widgets>
            <widget isCollapsed='false'>
              <linkstype>interntlink</linkstype>
              <boxlinktekst>test af internt link</boxlinktekst>
              <eksternelinks />
              <interntlink>
                <contentPicker>
                  <content>1320</content>
                </contentPicker>
              </interntlink>
            </widget>
            <widget isCollapsed='false'>
              <linkstype>eksternelinks</linkstype>
              <boxlinktekst>test af eksterne link</boxlinktekst>
              <eksternelinks>http://www.onkelb.dk</eksternelinks>;
              <interntlink>
                <contentPicker />
              </interntlink>
            </widget>
          </widgets>
        </linkBox>");
    
    var links2 = from x in root2.Elements("widgets").Elements("widget")
                 where x.Element("eksternelinks").Value != "" || x.Element("interntlink").Element("contentPicker").Descendants("content").Count() > 0
    
                 select x;
    

    links1 should return empty

    links2 should return 2 items

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies