473,396 Members | 1,965 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 10001
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Philipp Lenssen | last post by:
I have some chapters a document goes through, so at the end of each document I want to output "Continue with XYZ", where XYZ is the next-sibling of the current one I select. However I don't know...
7
by: steve bull | last post by:
I have the following code snippet to read the colorRange attributes for the colorRangeSwatch in the xml file listed below. string expr = "/swatches/colorRangeSwatch/colorRange";...
3
by: ArmsTom | last post by:
I've been playing around with xml for a couple days, so I have no idea what I'm doing yet :). Be gentle. Ok, here's what I have... ================================ Do while...
0
by: Banski | last post by:
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...
2
by: yonta via .NET 247 | last post by:
Hi I need to select several nodes (title) on an xml document based on an atrribute(user) and then read from each selected element (title) another attribute(nomequiz). Here's my code so far but...
5
by: Chua Wen Ching | last post by:
Hi, I read from this tutorial at codeproject Question A: http://www.codeproject.com/csharp/GsXPathTutorial.asp regarding xpath.. but i try to apply in my situation, and can't get it...
0
by: Luis Esteban Valencia Muñoz | last post by:
on my page I have a DropDownList and a label. My DropDownList is filled from an xml file using XPathNavigator. What I want to do is when the user selects a value from my DropDownList I want a...
5
by: =?Utf-8?B?VGFtbXkgTmVqYWRpYW4=?= | last post by:
Hi, I am using Visual C# window creating an application to read an xml file. At some point I need to use XPathNavigator and XPathNodeIterator however when I used those subjects I get error message...
0
by: gigs | last post by:
hi all! can someone explain me why im getting this result? this two <odjeljak<textshould be different <?xml version='1.0' encoding='utf-8' ?> <obrazac> <odjeljak> <odjeljak...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.