I'm using SyndicationFeed to read a National Weather Service XML file. An example alert XML file is at
https://github.com/mrcoulson/NWS-Alerts/blob/master/testwarning.xml. Currently, to determine if there is an alert, I'm checking to see if the title element contains some text
(in this case, "no active"). I'd much rather test for the existence of one of those elements that start with "cap", like <cap:event> and also reference that for the title of the alert. I have tried, but can't seem to understand how to reference non-standard
namespaces. Specifically, I guess I need to be able to reference "xmlns:cap ='urn:oasis:names:tc:emergency:cap:1.1'", but that's not even a URL.
Here's the relevant code as I'm using it now:
XmlReader reader = XmlReader.Create(strWarningFeed);
SyndicationFeed feed = SyndicationFeed.Load(reader);
// Read through the XML, pull out the items we need depending on whether or not there is a warning.
foreach (var str in feed.Items)
{
if (str.Title.Text.Contains("no active"))
{
weatherWarning.Visible = false;
}
else
{
string strTitle = str.Title.Text;
string strId = str.Id.ToString();
strTitle = strTitle.Substring(0, strTitle.LastIndexOf("issued"));
litOut.Text += String.Format("<p class=\"warningText\">{0}</p><p class=\"warningText\"><a href=\"{1}\">Read more.</a></p>", strTitle, strId);
}
}
Go easy on me. My understanding of the finer points of XML is pretty much not there.
Thanks!
"Though your ship be sturdy, no mercy has the sea."
-Genesis
I don't think I understand the example or how it can be used with SyndicationFeed. Can it?
Jeremy
Hi,
Of course it can!You can just use the XmlNameSpaceManager and add the namespace,please see this sample:
String xmlFrag = "<book>" +
"<title>Pride And Prejudice</title>" +
"<author>" +
"<first-name>Jane</first-name>" +
"<last-name>Austen</last-name>" +
"</author>" +
"<curr:price>19.95</curr:price>" +
"<misc>&h;</misc>" +
"</book>";
XmlNamespaceManager nsmgr = new XmlNamespaceManager(nt);
nsmgr.AddNamespace("curr", "urn:samples:dollar"); //Look at curr, because it's a namespace. Please use your own to change that
You should change the "urn:sample:dollar" to the url of "urn:oasis:names:tc:emergency:cap:1.1".
JeremyCoulso...
Member
43 Points
47 Posts
Reading non-standard namespaces with SyndicationFeed
Jul 27, 2012 05:50 PM|LINK
Hello to everyone. To make it easy, you can see all of the code for this on my GitHub repo: https://github.com/mrcoulson/NWS-Alerts/blob/master/Default.aspx.cs.
I'm using SyndicationFeed to read a National Weather Service XML file. An example alert XML file is at https://github.com/mrcoulson/NWS-Alerts/blob/master/testwarning.xml. Currently, to determine if there is an alert, I'm checking to see if the title element contains some text (in this case, "no active"). I'd much rather test for the existence of one of those elements that start with "cap", like <cap:event> and also reference that for the title of the alert. I have tried, but can't seem to understand how to reference non-standard namespaces. Specifically, I guess I need to be able to reference "xmlns:cap = 'urn:oasis:names:tc:emergency:cap:1.1'", but that's not even a URL.
Here's the relevant code as I'm using it now:
XmlReader reader = XmlReader.Create(strWarningFeed); SyndicationFeed feed = SyndicationFeed.Load(reader); // Read through the XML, pull out the items we need depending on whether or not there is a warning. foreach (var str in feed.Items) { if (str.Title.Text.Contains("no active")) { weatherWarning.Visible = false; } else { string strTitle = str.Title.Text; string strId = str.Id.ToString(); strTitle = strTitle.Substring(0, strTitle.LastIndexOf("issued")); litOut.Text += String.Format("<p class=\"warningText\">{0}</p><p class=\"warningText\"><a href=\"{1}\">Read more.</a></p>", strTitle, strId); } }Go easy on me. My understanding of the finer points of XML is pretty much not there.
Thanks!
-Genesis
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Reading non-standard namespaces with SyndicationFeed
Jul 29, 2012 03:19 AM|LINK
Hi,
If one of the xml nodes doesn't with an xml namespace name and you want to fetch it, just rename it and then use it as usual——
For more about this,you can refer this sample:
http://msdn.microsoft.com/en-us/library/system.xml.xmlnamespacemanager.addnamespace
JeremyCoulso...
Member
43 Points
47 Posts
Re: Reading non-standard namespaces with SyndicationFeed
Jul 30, 2012 01:05 PM|LINK
I don't think I understand the example or how it can be used with SyndicationFeed. Can it?
Jeremy
-Genesis
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Reading non-standard namespaces with SyndicationFeed
Jul 31, 2012 01:17 AM|LINK
Hi,
Of course it can!You can just use the XmlNameSpaceManager and add the namespace,please see this sample:
String xmlFrag = "<book>" + "<title>Pride And Prejudice</title>" + "<author>" + "<first-name>Jane</first-name>" + "<last-name>Austen</last-name>" + "</author>" + "<curr:price>19.95</curr:price>" + "<misc>&h;</misc>" + "</book>"; XmlNamespaceManager nsmgr = new XmlNamespaceManager(nt); nsmgr.AddNamespace("curr", "urn:samples:dollar"); //Look at curr, because it's a namespace. Please use your own to change thatYou should change the "urn:sample:dollar" to the url of "urn:oasis:names:tc:emergency:cap:1.1".