473,545 Members | 2,113 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How To get Attributes of an XML Element Using XPATH

Does anyone have any code to get Attributes of an XML Element Using
XPATH. I have been trying all day and cannot get this to work. I have
been using XPathNavigator, XPathDocument and the XPathNodeIterat or
trying to get the url attribute of the following XML:

<?xml version='1.0' encoding='utf-8' ?>
<Message>
<Internet>
<site url='http://www.yahoo.com' />
</Internet>
</Message>
Code Below:
XmlTextReader reader = new XmlTextReader(n ew
StringReader(xm l));

docNav = new XPathDocument(r eader);

nav = docNav.CreateNa vigator();

System.Xml.XPat h.XPathExpressi on exp = nav.Compile("//
Message/Internet/@url");
string strurl = Convert.ToStrin g(nav.Evaluate( exp));

Please Help. Thanks

Apr 10 '07 #1
1 11271
don't you want:

System.Xml.XPat h.XPathExpressi on exp =
nav.Compile("Me ssage/Internet/site/@url");

Rick
"BillGatesF an" <kl******@hotma il.comwrote in message
news:11******** **************@ y5g2000hsa.goog legroups.com...
Does anyone have any code to get Attributes of an XML Element Using
XPATH. I have been trying all day and cannot get this to work. I have
been using XPathNavigator, XPathDocument and the XPathNodeIterat or
trying to get the url attribute of the following XML:

<?xml version='1.0' encoding='utf-8' ?>
<Message>
<Internet>
<site url='http://www.yahoo.com' />
</Internet>
</Message>
Code Below:
XmlTextReader reader = new XmlTextReader(n ew
StringReader(xm l));

docNav = new XPathDocument(r eader);

nav = docNav.CreateNa vigator();

System.Xml.XPat h.XPathExpressi on exp = nav.Compile("//
Message/Internet/@url");
string strurl = Convert.ToStrin g(nav.Evaluate( exp));

Please Help. Thanks

Apr 11 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1966
by: wooks | last post by:
I have some information embedded in included schemas which I want to access at run-time for the purposes of contructing a GUI (they will support field labels and tool tips). The options seem to be to store them as documentation/appinfo elements within the included schema or as unhandled attributes. Because the schemas are included it...
11
3439
by: Jeff Kish | last post by:
Just wondering if it was possible to xquery an xml file to retrieve all elements that had a given text value or all elements where any attribute had a value of interest. I guess I could figure out a roughshod way to get the elements but I'm not sure about the attributes Thanks
2
3383
by: Jordan Willms | last post by:
Hi there. I am working with lom metadata and I am a little confused with how to form the following xml element: <lom xmlns="http://www.imsglobal.org/xsd/imsmd_v1p2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.imsglobal.org/xsd/imsmd_v1p2 imsmd_v1p2p2.xsd"/>
2
1327
by: monsterpot | last post by:
Hello, I have a document like this: <parameters> <param ptype="init">somedata</param> <param ptype="process">moredata</param> <param ptype="kill">alsodata</param> </parameters> <param> has the following definition in my XSD:
3
1727
by: Rohit Sharma | last post by:
Hi all.. ..NET + MSXML platform....VB Need to build a list of all the entities and attributes to allow the user to do search...how can i do that from the schema ?? Cheers Rohit
3
6850
by: Cheryl | last post by:
I need a sample that shows how can loop through the attributes and display the data.(.net / c#) <?xml version="1.0" encoding="UTF-8"?> <riders> <rider> <prename id=>Lance</prename> <surname>Armstrong</surname> </rider> <rider>
2
1185
by: AJ | last post by:
I have a problem that I suspect shouldn't be this hard to figure out, but I'm a .NET newbie so I'm looking for help. I am reading from an XML file to populate a combo box with the value of an "Name" attribute to several elements. When the user selects an item from the combo and clicks a command button, my application needs to: 1. Find...
2
1737
by: matthewjbarr | last post by:
Sorry for cross-posting, but I posted this in the PHP group and think it probably should have been here - think maybe I should be looking at XPath expressions to solve the problem. E.g. something like this: $item->xpath("//movie:image@src") *** Hi there,
2
2319
by: mnickel67 | last post by:
Hi, I'm still learning, so apologies if this is trivial... Given the following XML snippet: <Record> <foo id="a">... </foo> <foo id="b">....</foo> <foo id="c">.....</foo>
0
7468
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7401
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7656
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7808
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7423
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5972
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3450
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3443
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1884
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.