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>
}
}
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.:
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:
And im trying to render it using the following partial:
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.:and:
Let me know if that works!
/Chriztian
Will you marry me?
Haha - no; I'm already accounted for :-)
is working on a reply...