Hi,
Im quite new to XML in .Net.
Im getting values from an xml file using XPath and sorting as you see
in the code below.
I cant figure out how to get the value of date with GetAttribute.
Hope that this short description if enough and you can help to sort
out this problem.
Thanks in advance banski
Code:
public static void GetXml()
{
XPathDocument doc = new XPathDocument "xmlfeed.xml");
XPathNavigator nav = doc.CreateNavigator();
XPathExpression expr;
expr = nav.Compile("/root/body/articles/article");
//Sort by Date.
expr.AddSort("published/@date", XmlSortOrder.Descending,
XmlCaseOrder.None, "", XmlDataType.Text);
XPathNodeIterator iterator = nav.Select(expr);
while (iterator.MoveNext())
{
HttpContext.Current.Response.Write("ID = ");
HttpContext.Current.Response.Write(iterator.Curren t.GetAttribute("id",
String.Empty));
HttpContext.Current.Response.Write(" Date = ");
HttpContext.Current.Response.Write(iterator.Curren t.GetAttribute("date",
String.Empty));
}
}
Xml:
<?xml version="1.0" encoding="iso-8859-1"?>
<root>
<head>
<title>testing</title>
<description>Testing xml feed</description>
<lastupdate date="2004-02-02"/>
</head>
<body>
<articles>
<article id="1">
<published date="2004-02-02"/>
<article_name><![CDATA[This i a new article]]></headline>
<location href="http://test.com"/>
</article>
<article id="2">
<published date="2004-02-01"/>
<article_name><![CDATA[This is a second article]]></headline>
<location href="http://test2.com"/>
</article>
</articles>
</body>
</root>