472,096 Members | 1,184 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,096 software developers and data experts.

XPath and GetAttribute

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>
Nov 12 '05 #1
6 9911
"Banski" <th********@hotmail.com> wrote in message news:a1**************************@posting.google.c om...
I cant figure out how to get the value of date with GetAttribute. : : 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)); : : <article id="2">
<published date="2004-02-01"/>


First thing that jumps out at me, is the use of the Current XPathNavigator on the
XPathNodeIterator to retrieve both properties without moving. The 'id' and 'date'
attributes are not on the same nodes, therefore, they can't both be available from
Current without it being re-positioned. GetAttribute( ) does not perform a search
on child elements ... it will only look on the element where it has been positioned.

Next, the <published> element is only a child of the <article> element that you've
selected, therefore it won't directly exist in the XPathNodeIterator. The code must
go a level deeper using the Current XPathNavigator to fetch it for itself.

- - -
// . . .
string idValue = iterator.Current.GetAttribute( "id", String.Empty));
if ( idValue.Length != 0 )
{
HttpContext.Current.Response.Write( "ID = ");
HttpContext.Current.Response.Write( idValue);
if ( iterator.Current.MoveToFirstChild( ) ) // [1]
{
System.Diagnostics.Debug.Assert( iterator.Current.LocalName == "published"); // [2]

string dateValue = iterator.Current.GetAttribute( "date", String.Empty));
if ( dateValue.Length != 0 ) // [3]
{
HttpContext.Current.Response.Write( " Date = ");
HttpContext.Current.Response.Write( dateValue);
}
}
}
// . . .
- - -

This replacement for the original code segment quoted above should be a little
more robust.

Key points are:

1. After emitting an attribute value for the 'id' attribute (if there was one), the
XPathNavigator on iterator.Current is advanced to the first child node of
or the <article> element.

2. I make an assertion that my inference of the <publisher> element always
being the first child of <article> is correct. When debugging, the IDDE
will warn me if I'm mistaken (if there's any likelihood the <publisher> element
could be elsewhere; detecting it and possibly throwing an Exception might be
in order).

3. After checking if there is a 'date' attribute on the new element (GetAttribute( )
indicates this by returning a zero-length, empty string), any date value is written
out (additional validation of the date may be in order, using DateTime.Parse( )
and any relevant cultural date time formatting context).
Derek Harmon
Nov 12 '05 #2
"Banski" <th********@hotmail.com> wrote in message news:a1**************************@posting.google.c om...
I cant figure out how to get the value of date with GetAttribute. : : 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)); : : <article id="2">
<published date="2004-02-01"/>


First thing that jumps out at me, is the use of the Current XPathNavigator on the
XPathNodeIterator to retrieve both properties without moving. The 'id' and 'date'
attributes are not on the same nodes, therefore, they can't both be available from
Current without it being re-positioned. GetAttribute( ) does not perform a search
on child elements ... it will only look on the element where it has been positioned.

Next, the <published> element is only a child of the <article> element that you've
selected, therefore it won't directly exist in the XPathNodeIterator. The code must
go a level deeper using the Current XPathNavigator to fetch it for itself.

- - -
// . . .
string idValue = iterator.Current.GetAttribute( "id", String.Empty));
if ( idValue.Length != 0 )
{
HttpContext.Current.Response.Write( "ID = ");
HttpContext.Current.Response.Write( idValue);
if ( iterator.Current.MoveToFirstChild( ) ) // [1]
{
System.Diagnostics.Debug.Assert( iterator.Current.LocalName == "published"); // [2]

string dateValue = iterator.Current.GetAttribute( "date", String.Empty));
if ( dateValue.Length != 0 ) // [3]
{
HttpContext.Current.Response.Write( " Date = ");
HttpContext.Current.Response.Write( dateValue);
}
}
}
// . . .
- - -

This replacement for the original code segment quoted above should be a little
more robust.

Key points are:

1. After emitting an attribute value for the 'id' attribute (if there was one), the
XPathNavigator on iterator.Current is advanced to the first child node of
or the <article> element.

2. I make an assertion that my inference of the <publisher> element always
being the first child of <article> is correct. When debugging, the IDDE
will warn me if I'm mistaken (if there's any likelihood the <publisher> element
could be elsewhere; detecting it and possibly throwing an Exception might be
in order).

3. After checking if there is a 'date' attribute on the new element (GetAttribute( )
indicates this by returning a zero-length, empty string), any date value is written
out (additional validation of the date may be in order, using DateTime.Parse( )
and any relevant cultural date time formatting context).
Derek Harmon
Nov 12 '05 #3
Thanks Derek,

This solved my problem and thanks alot for the explanation.

Best regards
Banski

"Derek Harmon" <lo*******@msn.com> wrote in message news:<up**************@TK2MSFTNGP12.phx.gbl>...
"Banski" <th********@hotmail.com> wrote in message news:a1**************************@posting.google.c om...
I cant figure out how to get the value of date with GetAttribute.

: :
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));

: :
<article id="2">
<published date="2004-02-01"/>


First thing that jumps out at me, is the use of the Current XPathNavigator on the
XPathNodeIterator to retrieve both properties without moving. The 'id' and 'date'
attributes are not on the same nodes, therefore, they can't both be available from
Current without it being re-positioned. GetAttribute( ) does not perform a search
on child elements ... it will only look on the element where it has been positioned.

Next, the <published> element is only a child of the <article> element that you've
selected, therefore it won't directly exist in the XPathNodeIterator. The code must
go a level deeper using the Current XPathNavigator to fetch it for itself.

- - -
// . . .
string idValue = iterator.Current.GetAttribute( "id", String.Empty));
if ( idValue.Length != 0 )
{
HttpContext.Current.Response.Write( "ID = ");
HttpContext.Current.Response.Write( idValue);
if ( iterator.Current.MoveToFirstChild( ) ) // [1]
{
System.Diagnostics.Debug.Assert( iterator.Current.LocalName == "published"); // [2]

string dateValue = iterator.Current.GetAttribute( "date", String.Empty));
if ( dateValue.Length != 0 ) // [3]
{
HttpContext.Current.Response.Write( " Date = ");
HttpContext.Current.Response.Write( dateValue);
}
}
}
// . . .
- - -

This replacement for the original code segment quoted above should be a little
more robust.

Key points are:

1. After emitting an attribute value for the 'id' attribute (if there was one), the
XPathNavigator on iterator.Current is advanced to the first child node of
or the <article> element.

2. I make an assertion that my inference of the <publisher> element always
being the first child of <article> is correct. When debugging, the IDDE
will warn me if I'm mistaken (if there's any likelihood the <publisher> element
could be elsewhere; detecting it and possibly throwing an Exception might be
in order).

3. After checking if there is a 'date' attribute on the new element (GetAttribute( )
indicates this by returning a zero-length, empty string), any date value is written
out (additional validation of the date may be in order, using DateTime.Parse( )
and any relevant cultural date time formatting context).
Derek Harmon

Nov 12 '05 #4
Thanks Derek,

This solved my problem and thanks alot for the explanation.

Best regards
Banski

"Derek Harmon" <lo*******@msn.com> wrote in message news:<up**************@TK2MSFTNGP12.phx.gbl>...
"Banski" <th********@hotmail.com> wrote in message news:a1**************************@posting.google.c om...
I cant figure out how to get the value of date with GetAttribute.

: :
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));

: :
<article id="2">
<published date="2004-02-01"/>


First thing that jumps out at me, is the use of the Current XPathNavigator on the
XPathNodeIterator to retrieve both properties without moving. The 'id' and 'date'
attributes are not on the same nodes, therefore, they can't both be available from
Current without it being re-positioned. GetAttribute( ) does not perform a search
on child elements ... it will only look on the element where it has been positioned.

Next, the <published> element is only a child of the <article> element that you've
selected, therefore it won't directly exist in the XPathNodeIterator. The code must
go a level deeper using the Current XPathNavigator to fetch it for itself.

- - -
// . . .
string idValue = iterator.Current.GetAttribute( "id", String.Empty));
if ( idValue.Length != 0 )
{
HttpContext.Current.Response.Write( "ID = ");
HttpContext.Current.Response.Write( idValue);
if ( iterator.Current.MoveToFirstChild( ) ) // [1]
{
System.Diagnostics.Debug.Assert( iterator.Current.LocalName == "published"); // [2]

string dateValue = iterator.Current.GetAttribute( "date", String.Empty));
if ( dateValue.Length != 0 ) // [3]
{
HttpContext.Current.Response.Write( " Date = ");
HttpContext.Current.Response.Write( dateValue);
}
}
}
// . . .
- - -

This replacement for the original code segment quoted above should be a little
more robust.

Key points are:

1. After emitting an attribute value for the 'id' attribute (if there was one), the
XPathNavigator on iterator.Current is advanced to the first child node of
or the <article> element.

2. I make an assertion that my inference of the <publisher> element always
being the first child of <article> is correct. When debugging, the IDDE
will warn me if I'm mistaken (if there's any likelihood the <publisher> element
could be elsewhere; detecting it and possibly throwing an Exception might be
in order).

3. After checking if there is a 'date' attribute on the new element (GetAttribute( )
indicates this by returning a zero-length, empty string), any date value is written
out (additional validation of the date may be in order, using DateTime.Parse( )
and any relevant cultural date time formatting context).
Derek Harmon

Nov 12 '05 #5
Hi again,

I how do I get the attribute of second child location href attribute?

Best regards
Banski

"Derek Harmon" <lo*******@msn.com> wrote in message news:<up**************@TK2MSFTNGP12.phx.gbl>...
"Banski" <th********@hotmail.com> wrote in message news:a1**************************@posting.google.c om...
I cant figure out how to get the value of date with GetAttribute.

: :
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));

: :
<article id="2">
<published date="2004-02-01"/>


First thing that jumps out at me, is the use of the Current XPathNavigator on the
XPathNodeIterator to retrieve both properties without moving. The 'id' and 'date'
attributes are not on the same nodes, therefore, they can't both be available from
Current without it being re-positioned. GetAttribute( ) does not perform a search
on child elements ... it will only look on the element where it has been positioned.

Next, the <published> element is only a child of the <article> element that you've
selected, therefore it won't directly exist in the XPathNodeIterator. The code must
go a level deeper using the Current XPathNavigator to fetch it for itself.

- - -
// . . .
string idValue = iterator.Current.GetAttribute( "id", String.Empty));
if ( idValue.Length != 0 )
{
HttpContext.Current.Response.Write( "ID = ");
HttpContext.Current.Response.Write( idValue);
if ( iterator.Current.MoveToFirstChild( ) ) // [1]
{
System.Diagnostics.Debug.Assert( iterator.Current.LocalName == "published"); // [2]

string dateValue = iterator.Current.GetAttribute( "date", String.Empty));
if ( dateValue.Length != 0 ) // [3]
{
HttpContext.Current.Response.Write( " Date = ");
HttpContext.Current.Response.Write( dateValue);
}
}
}
// . . .
- - -

This replacement for the original code segment quoted above should be a little
more robust.

Key points are:

1. After emitting an attribute value for the 'id' attribute (if there was one), the
XPathNavigator on iterator.Current is advanced to the first child node of
or the <article> element.

2. I make an assertion that my inference of the <publisher> element always
being the first child of <article> is correct. When debugging, the IDDE
will warn me if I'm mistaken (if there's any likelihood the <publisher> element
could be elsewhere; detecting it and possibly throwing an Exception might be
in order).

3. After checking if there is a 'date' attribute on the new element (GetAttribute( )
indicates this by returning a zero-length, empty string), any date value is written
out (additional validation of the date may be in order, using DateTime.Parse( )
and any relevant cultural date time formatting context).
Derek Harmon

Nov 12 '05 #6
Hi again,

I how do I get the attribute of second child location href attribute?

Best regards
Banski

"Derek Harmon" <lo*******@msn.com> wrote in message news:<up**************@TK2MSFTNGP12.phx.gbl>...
"Banski" <th********@hotmail.com> wrote in message news:a1**************************@posting.google.c om...
I cant figure out how to get the value of date with GetAttribute.

: :
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));

: :
<article id="2">
<published date="2004-02-01"/>


First thing that jumps out at me, is the use of the Current XPathNavigator on the
XPathNodeIterator to retrieve both properties without moving. The 'id' and 'date'
attributes are not on the same nodes, therefore, they can't both be available from
Current without it being re-positioned. GetAttribute( ) does not perform a search
on child elements ... it will only look on the element where it has been positioned.

Next, the <published> element is only a child of the <article> element that you've
selected, therefore it won't directly exist in the XPathNodeIterator. The code must
go a level deeper using the Current XPathNavigator to fetch it for itself.

- - -
// . . .
string idValue = iterator.Current.GetAttribute( "id", String.Empty));
if ( idValue.Length != 0 )
{
HttpContext.Current.Response.Write( "ID = ");
HttpContext.Current.Response.Write( idValue);
if ( iterator.Current.MoveToFirstChild( ) ) // [1]
{
System.Diagnostics.Debug.Assert( iterator.Current.LocalName == "published"); // [2]

string dateValue = iterator.Current.GetAttribute( "date", String.Empty));
if ( dateValue.Length != 0 ) // [3]
{
HttpContext.Current.Response.Write( " Date = ");
HttpContext.Current.Response.Write( dateValue);
}
}
}
// . . .
- - -

This replacement for the original code segment quoted above should be a little
more robust.

Key points are:

1. After emitting an attribute value for the 'id' attribute (if there was one), the
XPathNavigator on iterator.Current is advanced to the first child node of
or the <article> element.

2. I make an assertion that my inference of the <publisher> element always
being the first child of <article> is correct. When debugging, the IDDE
will warn me if I'm mistaken (if there's any likelihood the <publisher> element
could be elsewhere; detecting it and possibly throwing an Exception might be
in order).

3. After checking if there is a 'date' attribute on the new element (GetAttribute( )
indicates this by returning a zero-length, empty string), any date value is written
out (additional validation of the date may be in order, using DateTime.Parse( )
and any relevant cultural date time formatting context).
Derek Harmon

Nov 12 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Philipp Lenssen | last post: by
3 posts views Thread by ArmsTom | last post: by
reply views Thread by Banski | last post: by
2 posts views Thread by yonta via .NET 247 | last post: by
5 posts views Thread by Chua Wen Ching | last post: by
reply views Thread by Luis Esteban Valencia Muñoz | last post: by
5 posts views Thread by =?Utf-8?B?VGFtbXkgTmVqYWRpYW4=?= | last post: by
reply views Thread by gigs | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.