I've got a problem getting the value of an xml tag with a namespace.
Here's The XML:
<rss xmlns:obs="http://www.cision.com/" xmlns:content="http://purl.org/rss/1.0/modules/content/" version="2.0">
<channel>
<item obs:id="1">
<title>Title 1</title>
<content:encoded>Lorem ipsum dolar sit amet 1</content:encoded>
</item>
<item obs:id="2">
<title>Title 2</title>
<content:encoded>Lorem ipsum dolar sit amet 2</content:encoded>
</item>
</channel>
</rss>
Here's my Razor macro in a template:
<umbraco:Macro runat="server" language="cshtml">
@{
var targetUri = new Uri("http://www.example.com/example.xml");
var request = (HttpWebRequest)WebRequest.Create(targetUri);
if ((request.GetResponse().ContentLength > 0))
{
var stream = new StreamReader(request.GetResponse().GetResponseStream());
xml = stream.ReadToEnd();
if (stream != null)
{
stream.Close();
}
}
var feed = Library.ToDynamicXml(xml);
foreach (var post in feed.channel.item)
{
@post.title <-- WORKS
}
}
</umbraco:Macro>
In the foreach loop getting the <title> tag works great with @post.title. But how do i get the value of <content:encoded>?
There's a similar problem when a node contans a dash. In that case (example: <link-title>) the invalid character gets removed (@post.linktitle), so hopefully it's the same here.
The same way as above. Perhaps it would be more usefull to give some explanation how it works. The local-name() funtion refers to the part of the node's or attribute's name without the namespace prefix if there's any. So when you spell e.g.:
Hi, Peter. The XPath method of DynamicXml is supposed to work only under condition that the XPath query passed returns the set of elements, not attributes. In an ordinal namespaceless case you get attributes just accessing them as dynamic properties. Unfortunately it doesn't work when there's a namespace assigned to the attribute. You can workaround it accessing the DynamicXml.BaseElement property that return a raw XElement underlying to the DynamicXml.
Great! Had no idea that there was a BaseElement property in DynamicXml. Umbraco is badly documented about these kind of things. Thank you so much Rodion.
XML - Getting the value of tag with namespace
Hello,
I've got a problem getting the value of an xml tag with a namespace.
Here's The XML:
Here's my Razor macro in a template:
<umbraco:Macro runat="server" language="cshtml"> @{ var targetUri = new Uri("http://www.example.com/example.xml"); var request = (HttpWebRequest)WebRequest.Create(targetUri); if ((request.GetResponse().ContentLength > 0)) { var stream = new StreamReader(request.GetResponse().GetResponseStream()); xml = stream.ReadToEnd(); if (stream != null) { stream.Close(); } } var feed = Library.ToDynamicXml(xml); foreach (var post in feed.channel.item) { @post.title <-- WORKS } } </umbraco:Macro>
In the foreach loop getting the <title> tag works great with @post.title. But how do i get the value of <content:encoded>?
Thanks in advance!
/Peter
Try either @post.content or @post.contentencoded
There's a similar problem when a node contans a dash. In that case (example: <link-title>) the invalid character gets removed (@post.linktitle), so hopefully it's the same here.
Hi Sebastiaan,
Thanks for your answer but I've already tried that. Both breaks Razor and outputs following:
Error loading Razor Script
'umbraco.MacroEngines.DynamicXml' does not contain a definition for 'contentencoded'
Any other suggestions?
Hi, Peter. Although it doesn't look as elegant as it could but you can always query your DynamicXml by usual XPath:
I can't seem to find a way to reference the <content:encoded> tag directly, but I was able to get to it like this:
Hope that helps!
Great!!
Thanks Rodion and Douglas. Both of your examples worked. But I finally I chose to use XPath like this:
Sorry for lending this thread. But you guys maybe can help me?
How am I supposed to do to get the id from the item
<itemobs:id="1">
<itemobs:id="2">
<itemobs:id="3">
ans so on.
Thanks in advance!
The same way as above. Perhaps it would be more usefull to give some explanation how it works. The local-name() funtion refers to the part of the node's or attribute's name without the namespace prefix if there's any. So when you spell e.g.:
then it reads: "Give me all the attributes of the current node whose name without a namespace prefix is equal to the string 'id'".
Really frustrating. I'm trying to get the value of the attribute obs:id. But can't get it to work.
@Nick - Did you manage to get the value of attribute?
@Rodion - I tried your suggestion to use the same xpath as above for the node. But only got errors.
Attempt 1:
Error output:
Attempt 2:
error output
What am i doing wrong?
I've added following @using to my macro. Still attempt 2 outputs unexpected type. Why?
<umbraco:macro>
@using System.Linq
@using System.Xml.Linq
@using System.Xml.XPath
...
I use .InnerText after the Xpath-expression to get the value of a tag. What should i use to get value of an attribute?
Thanks
Hi, Peter. The XPath method of DynamicXml is supposed to work only under condition that the XPath query passed returns the set of elements, not attributes. In an ordinal namespaceless case you get attributes just accessing them as dynamic properties. Unfortunately it doesn't work when there's a namespace assigned to the attribute. You can workaround it accessing the DynamicXml.BaseElement property that return a raw XElement underlying to the DynamicXml.
Below is the sample of working code:
Great! Had no idea that there was a BaseElement property in DynamicXml. Umbraco is badly documented about these kind of things. Thank you so much Rodion.
is working on a reply...