Copied to clipboard

Flag this post as spam?

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


  • Claushingebjerg 939 posts 2574 karma points
    Oct 27, 2016 @ 07:39
    Claushingebjerg
    0

    rendering external xml with razor

    I have previousely succeslfully rendered xml with razor using Blakes guide at http://letswritecode.net/articles/rss-to-html-with-razor/ but i've hit a dead end on an xml file, which i cant seem to render. And i cant figure out why.

    I have the following xml structure:

    <?xml version="1.0" encoding="utf-8"?>
    <ArrayOfSmsTowebMessage xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.beredskabsalarm.dk/">
      <smsTowebMessage>
        <Id>39926</Id>
        <UserName>a username</UserName>
        <smsGroupId xsi:nil="true" />
        <FromDateTime>2016-10-26T23:30:00</FromDateTime>
        <ToDateTime>2016-10-29T00:00:00</ToDateTime>
        <Header>This is a test</Header>
        <Message>This is a test message</Message>
        <CriticalStatus>false</CriticalStatus>
        <Attention>false</Attention>
        <Completed xsi:nil="true" />
        <TypeId xsi:nil="true" />
      </smsTowebMessage>
      <smsTowebMessage>
        <Id>39927</Id>
        <UserName>another username</UserName>
        <smsGroupId xsi:nil="true" />
        <FromDateTime>2016-10-26T23:30:00</FromDateTime>
        <ToDateTime>2016-10-29T00:00:00</ToDateTime>
        <Header>This is another test</Header>
        <Message>This is another test message</Message>
        <CriticalStatus>false</CriticalStatus>
        <Attention>false</Attention>
        <Completed xsi:nil="true" />
        <TypeId xsi:nil="true" />
      </smsTowebMessage>
    </ArrayOfSmsTowebMessage>
    

    And im trying to render it using the following partial:

    @inherits Umbraco.Web.Mvc.UmbracoViewPage
    @using System.Xml;
    
    @{
        var url = "http://www.thexmlurl.com/status.xml";
        int i = 1;
        var amt = 5;
        if (url != "")
        {
            XmlDocument xml = new XmlDocument();
            xml.Load(url);
            XmlNodeList nodes = xml.SelectNodes("//smsTowebMessage");
    
            <ul class="rss-list">
                @foreach (XmlNode node in nodes)
                {
                    if (i <= amt)
                    {
                        var title = node.SelectSingleNode("Header").InnerText;
                        var body = node.SelectSingleNode("Message").InnerText;
                        <li>
                           <h3>@title</h3>
                           <p>@body</p>
                        </li>
                    }
                    i++;
                }
            </ul>
        }
    }
    
  • Chriztian Steinmeier 2800 posts 8791 karma points MVP 8x admin c-trib
    Oct 28, 2016 @ 08:53
    Chriztian Steinmeier
    100

    Hi Claus,

    It's most likely because there's a default namespace on the XML, which means you'll need to handle that somehow (I don't really know how it's done correctly in Razor/C# though).

    To do it "correct", you'll need to look up something called a NamespaceManager (or that's what I think it's called).

    OR - if you need this by yesterday, you can just make your XPaths infinitely more difficult to read, by using the local-name() function instead of using the elements' names, e.g.:

    XmlNodeList nodes = xml.SelectNodes("//*[local-name() = 'smsTowebMessage']");
    

    and:

    var title = node.SelectSingleNode("*[local-name() = 'Header']").InnerText;
    var body = node.SelectSingleNode("*[local-name() = 'Message']").InnerText;
    

    Let me know if that works!

    /Chriztian

  • Claushingebjerg 939 posts 2574 karma points
    Oct 28, 2016 @ 10:22
    Claushingebjerg
    0

    Will you marry me?

  • Chriztian Steinmeier 2800 posts 8791 karma points MVP 8x admin c-trib
    Oct 28, 2016 @ 10:27
    Chriztian Steinmeier
    0

    Haha - no; I'm already accounted for :-)

  • 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