By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,812 Members | 863 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,812 IT Pros & Developers. It's quick & easy.

Getting attribute value from xml using xpath to get file path

P: n/a
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
Share this Question
Share on Google+
1 Reply


P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.