473,396 Members | 1,785 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.

Getting attribute value from xml using xpath to get file path

I have the following xml file:
<?xml version="1.0" encoding="utf-8" ?>
<course id="2555" title="Developing Microsoft .NET Applications for Windows
(Visual C# .NET)" length="5 days"
source="http://www.microsoft.com/learning/syllabi/en-us/2555Afinal.mspx">
<module id="1" title="Introducing Windows Forms"
location="D:\Disk-C\Documents and Settings\orit_itzhar.ATRICA\My
Documents\XML\csharp">
<lesson id="1.1">
<subject>Creating a Form</subject>
<file>Introducing_Windows_Forms_Course-2555-Module-1.pdf</file>
</lesson>
<lesson id="1.2">
<subject>Adding Controls to a Form</subject>
<file>type&exceptions.pdf</file>
</lesson>
<lesson id="1.3">
<subject>Creating an Inherited Form</subject>
<file>winforms.pdf</file>
</lesson>
<lesson id="1.4">
<subject>Organizing Controls on a Form</subject>
<file>BnsLrnCs.zip</file>
</lesson>
<lesson id="1.5">
<subject>Creating MDI Applications</subject>
<file>Working_with_Controls_Course-2555-Module-2.pdf</file>
</lesson>
<lab id="1.1">
<exercise id="1.1.1">Creating a New Windows Form</exercise>
<exercise id="1.1.2">Inheriting a New Form from an Existing Windows
Form</exercise>
</lab>
</module>
<module id="2" title="Working with Controls" location="D:\Disk-C\Documents
and Settings\orit_itzhar.ATRICA\My Documents\XML\c#">
<lesson id="2.1">
<subject>Creating an Event Handler for a Control</subject>
<file>Introducing_Windows_Forms_Course-2555-Module-1.pdf</file>
</lesson>
<lesson id="2.2">
<subject>Using Windows Forms Controls</subject>
<file>type&exceptions.pdf</file>
</lesson>
<lesson id="2.3">
<subject>Using Dialog Boxes in a Windows Forms Application</subject>
<file>winforms.pdf</file>
</lesson>
<lesson id="2.4">
<subject>Adding Controls at Run Time</subject>
<file>BnsLrnCs.zip</file>
</lesson>
<lesson id="2.5">
<subject>Creating Menus</subject>
<file>Working_with_Controls_Course-2555-Module-2.pdf</file>
</lesson>
<lesson id="2.6">
<subject>Validating User Input</subject>
<file>Working_with_Controls_Course-2555-Module-2.pdf</file>
</lesson>
<lab id="2.1">
<exercise id="2.1.1">Creating and Using Controls</exercise>
</lab>
</module>
</course>

I want to get the path to file by getting the "location" attribute from the
"module" node, and concatenating the inner text of the "file" to it.
I tried the code below to get out the location into the courseDocumentsPath
varaiable:
XmlDocument doc = new XmlDocument();
doc.LoadXml("lessons.xml");

string courseDocumentsPath = null;

XmlNodeList nl = doc.SelectNodes("/course/module/@location");
foreach(XmlNode node in nl)
{
courseDocumentsPath = node.Value;
}

I get on this System error, Can someone please help what is the problem?

Thanks
Nov 12 '05 #1
1 2914
XmlDocument.LoadXml() loads a XML String, so it assumes that the
argument you pass in is valid XML, as in:

doc.LoadXml("<test>xyz</test>");

You want to use XmlDocument.Load(), which allows you to pass in a path:

doc.Load("Lessons.xml")

HTH,

Bennie Haelen

orit wrote:
I have the following xml file:
<?xml version="1.0" encoding="utf-8" ?>
<course id="2555" title="Developing Microsoft .NET Applications for Windows
(Visual C# .NET)" length="5 days"
source="http://www.microsoft.com/learning/syllabi/en-us/2555Afinal.mspx">
<module id="1" title="Introducing Windows Forms"
location="D:\Disk-C\Documents and Settings\orit_itzhar.ATRICA\My
Documents\XML\csharp">
<lesson id="1.1">
<subject>Creating a Form</subject>
<file>Introducing_Windows_Forms_Course-2555-Module-1.pdf</file>
</lesson>
<lesson id="1.2">
<subject>Adding Controls to a Form</subject>
<file>type&exceptions.pdf</file>
</lesson>
<lesson id="1.3">
<subject>Creating an Inherited Form</subject>
<file>winforms.pdf</file>
</lesson>
<lesson id="1.4">
<subject>Organizing Controls on a Form</subject>
<file>BnsLrnCs.zip</file>
</lesson>
<lesson id="1.5">
<subject>Creating MDI Applications</subject>
<file>Working_with_Controls_Course-2555-Module-2.pdf</file>
</lesson>
<lab id="1.1">
<exercise id="1.1.1">Creating a New Windows Form</exercise>
<exercise id="1.1.2">Inheriting a New Form from an Existing Windows
Form</exercise>
</lab>
</module>
<module id="2" title="Working with Controls" location="D:\Disk-C\Documents
and Settings\orit_itzhar.ATRICA\My Documents\XML\c#">
<lesson id="2.1">
<subject>Creating an Event Handler for a Control</subject>
<file>Introducing_Windows_Forms_Course-2555-Module-1.pdf</file>
</lesson>
<lesson id="2.2">
<subject>Using Windows Forms Controls</subject>
<file>type&exceptions.pdf</file>
</lesson>
<lesson id="2.3">
<subject>Using Dialog Boxes in a Windows Forms Application</subject>
<file>winforms.pdf</file>
</lesson>
<lesson id="2.4">
<subject>Adding Controls at Run Time</subject>
<file>BnsLrnCs.zip</file>
</lesson>
<lesson id="2.5">
<subject>Creating Menus</subject>
<file>Working_with_Controls_Course-2555-Module-2.pdf</file>
</lesson>
<lesson id="2.6">
<subject>Validating User Input</subject>
<file>Working_with_Controls_Course-2555-Module-2.pdf</file>
</lesson>
<lab id="2.1">
<exercise id="2.1.1">Creating and Using Controls</exercise>
</lab>
</module>
</course>

I want to get the path to file by getting the "location" attribute from the
"module" node, and concatenating the inner text of the "file" to it.
I tried the code below to get out the location into the courseDocumentsPath
varaiable:
XmlDocument doc = new XmlDocument();
doc.LoadXml("lessons.xml");

string courseDocumentsPath = null;

XmlNodeList nl = doc.SelectNodes("/course/module/@location");
foreach(XmlNode node in nl)
{
courseDocumentsPath = node.Value;
}

I get on this System error, Can someone please help what is the problem?

Thanks

Nov 12 '05 #2

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

Similar topics

7
by: svilen | last post by:
hello again. i'm now into using python instead of another language(s) for describing structures of data, including names, structure, type-checks, conversions, value-validations, metadata etc....
9
by: Iain | last post by:
I want to create an XML configuration file which might look like <REGION Name="Europe" WingDing="Blue"> <COUNTRY Name="UK" WingDing="white"> <TOWN Name="London" WingDing="Orange" /> </COUNTRY>...
5
by: Don | last post by:
Hi: I have created an xsd from my xml document. I pop this xsd in the following directory: C:\program files\Microsoft Visual Studio .NET 2003\Common7\Packages\schemas\xml. That give me...
3
by: Goran Djuranovic | last post by:
Hi All, Does anyone know how to retreive deepest XPath value from XML document by using VB.NET? For example, if I had an XML file like this: <Root> <Customer> <Name>MyName</Name> </Customer>...
2
by: Christian Rühl | last post by:
heyho, guys! here's another question for you now: i built an iterator to get all the nodes with a certain attribute in an xml dom. it all looks like this (i'm using .NET framework 1.1) ...
2
by: Bilal | last post by:
Hello, I'm stuck on this problem for quite some time and hope somebody would be able to guide me. Basically, I need to populate a large number of "template" XML files which have all...
0
by: kelvin273 | last post by:
Hi all, i'm new in this community (and in .NET programming) and i've a problem with xpathnavigator. The idea is to have an xml document with attribute editable by windows form. I write this...
0
by: buntyindia | last post by:
Hi, I have a very strange problem with my application. I have developed it using Struts. I have a TextBox With Some fixed value in it and on Submit iam passing it to another page. <html:form...
16
by: news | last post by:
I'm trying to parse an XML formatted post reply, and I can't figure out how to get the value of the right tag. The XML is formatted like this: <A> <foo>val1</foo> </A> <B> <foo>val2</foo>...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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
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.