Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

xmlDocument.SelectNodes : cannot extract data from the XML File

Status
Not open for further replies.

aSDDSasdas

Programmer
Jul 12, 2006
19
AT
i am trying to extract the nodes from the File with the following code. However i am not succesfull in extracting the codes from the xpath given below...i do not know wht is my mistake in the xpath as i always get temp =0

would be gratefull for any help

try
{
XmlDocument xDoc = new XmlDocument();

xDoc.Load("Test.xml");
XmlNodeList temp = xDoc.SelectNodes("/Project/Calendars/Calendar[Name='Siemens (Österreich)']WeekDays/WeekDay/Timeperiod/FromDate");

}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
i am having the following xml file

<?xml version="1.0" ?>
- <Project xmlns="<Name>Test.xml</Name>
<Title>MS-Project2000- Kalender</Title>
<Author>Competence Center Projektmanagement</Author>
<CreationDate>1998-09-10T13:43:00</CreationDate>
<LastSaved>2008-07-11T13:01:00</LastSaved>
..
...
...
- <Calendars>
- <Calendar>
<UID>11</UID>
<Name>Siemens (Österreich)</Name>
- <WeekDays>
- <WeekDay>
<DayType>0</DayType>
<DayWorking>0</DayWorking>
- <TimePeriod>
<FromDate>1998-04-13T00:00:00</FromDate>
 
You've to take care of the existence of the default namespace where the nodes reside in order to retrieve them through the xpath. Like this.
[tt]
XmlDocument xDoc = new XmlDocument();
xDoc.Load("Test.xml");
[blue]XmlNamespaceManage nsmgr=new XmlNamespaceManager(xDoc.NameTable);
nsmgr.AddNamespace("dns","[ignore][/ignore]");[/blue]
XmlNodeList temp = xDoc.SelectNodes("/[blue]dns:[/blue]Project/[blue]dns:[/blue]Calendars/[blue]dns:[/blue]Calendar[[blue]dns:[/blue]Name='Siemens (Österreich)'][red]/[/red][blue]dns:[/blue]WeekDays/[blue]dns:[/blue]WeekDay/[blue]dns:[/blue]Timeperiod/[blue]dns:[/blue]FromDate");
[/tt]
 
Amendment
Incomplete revision of the last line. It should be read like this.
[tt]
XmlNodeList temp = xDoc.SelectNodes("/[blue]dns:[/blue]Project/[blue]dns:[/blue]Calendars/[blue]dns:[/blue]Calendar[[blue]dns:[/blue]Name='Siemens (Österreich)'][red]/[/red][blue]dns:[/blue]WeekDays/[blue]dns:[/blue]WeekDay/[blue]dns:[/blue]Timeperiod/[blue]dns:[/blue]FromDate"[blue],nsmgr[/blue]);
[/tt]
 
thnax a lot its working except i needed to add a second parameter.
thanx again
 
>...its working except i needed to add a second parameter.
Sure, if you meant by that the parameter as shown in my amendment above.

Also you have typo, being propagated into the revision.
>dns:Timeperiod
should be read
[tt]dns:Time[red]P[/red]eriod[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top