473,385 Members | 1,375 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,385 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 9999
"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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.