469,898 Members | 1,945 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,898 developers. It's quick & easy.

C# XPathNavigator doesnt like attributes and wont find the data

Morning all from a newbie.

I have recently written an automation program (my first ever program) for testing and now I am trying to read XML data and assign the values to variables (rather than Excel). Using XPathNavigator I managed to read some of the data, but in order to do this I had to manually remove the attributes from the Parent Nodes. Going forward this isnt an option as we may be reading and running hundreds of files a day and so I ask for your help.

Here is my code:
Expand|Select|Wrap|Line Numbers
  1.             XPathDocument xpd = new XPathDocument(ReqFile);
  2.             XPathNavigator xpn = xpd.CreateNavigator();
  3.             XPathNodeIterator xpi = xpn.Select("/System/Case");
  4.  
  5.             Console.WriteLine("0, Current name = " + xpi.Current.Name);
  6.  
  7.             while (xpi.MoveNext()) // each testcase node
  8.             {
  9.                 Console.WriteLine("1, Current name = " + xpi.Current.Name);
  10.  
  11.                 XPathNodeIterator tcChild = xpi.Current.SelectChildren(XPathNodeType.Element);
  12.                 while (tcChild.MoveNext()) // each part (<inputs> and <expected>) of <testcase>
  13.                 {
  14.                     Console.WriteLine("2, tcChildName value = " + tcChild.Current.Value + ", tcChildName = " + tcChild.Current.Name);
  15.  

This will read and therefore write to the output window the following XML file:
[XML]
<?xml version="1.0" encoding="utf-8"?>
<System>
<Control xmlns="http://www.webaddress-here.co.uk/mysystem-pos/1.0">
<SystemVersion>1.0</SystemVersion>
</Control>
<Case>
<ReviewType />
<AdviceBasisType />
<CreatedDate>2008-04-07T15:11:39.747</CreatedDate>
<LastUpdatedDate>2008-04-07T15:13:59</LastUpdatedDate>
<Reference>XX-666</Reference>
<CheckedStatus />
<StatusHistory currentStatus="AgendaSetComplete">
<Status date="2008-04-07T15:11:40" type="AgendaSetIncomplete" />
<Status date="2008-04-07T15:12:13" type="AgendaSetComplete" />
</StatusHistory>
<Notes />
<AttitudeToRisk xmlns="http://www.webaddress-here.co.uk/mysystem-pos/1.0" xmlns:fa="http://www.webaddress-here.co.uk/mysystem-pos/1.0" questionnaireId="2">
<ClientATR code="Adventurous">Adventurous</ClientATR>
<Notes />
</AttitudeToRisk>
<FactFindQuestions />
<Parties>
<HasDependants code="Yes">Yes</HasDependants>
<Party>
<Forename>Harry</Forename>
[/XML]

but it wont read:
[XML]
<?xml version="1.0" encoding="utf-8"?>
<System xmlns="http://www.webaddress-here.co.uk/mysystem-pos/1.0">
<Control xmlns="http://www.webaddress-here.co.uk/mysystem-pos/1.0">
<SystemVersion>1.0</SystemVersion>
</Control>
<Case xmlns="http://www.www.webaddress-here.co.uk/mysystem-pos/1.0" id="XY45E4D1-654B-4BF3-BB26-1647FFECA021">
<ReviewType />
<AdviceBasisType />
<CreatedDate>2008-04-07T15:11:39.747</CreatedDate>
<LastUpdatedDate>2008-04-14T09:45:54</LastUpdatedDate>
<Reference>XX-666</Reference>
<CheckedStatus />
<StatusHistory currentStatus="AgendaSetComplete">
<Status date="2008-04-07T15:11:40" type="AgendaSetIncomplete" />
<Status date="2008-04-07T15:12:13" type="AgendaSetComplete" />
</StatusHistory>
<Notes />
<AttitudeToRisk xmlns="http://www.webaddress-here.co.uk/mysystem-pos/1.0" xmlns:fa="http://www.www.webaddress-here.co.uk/mysystem-pos/1.0" questionnaireId="2">
<ClientATR code="Adventurous">Adventurous</ClientATR>
<Notes />
</AttitudeToRisk>
<FactFindQuestions />
<Parties xmlns="http://www.www.webaddress-here.co.uk/mysystem-pos/1.0">
<HasDependants code="Yes">Yes</HasDependants>
<Party xmlns="http://www.webaddress-here.co.uk/mysystem-pos/1.0" id="ZZ1640D3-A45C-4650-A9C4-BC61C936ADBA" type="Client" status="Prospect" state="New">
<Forename>Harry</Forename>
[/XML]

Obviously I have missed out a lot of the XML files as they are huge but they are all correct in that they have closing tags.

I have tried changing my code:
XPathNodeIterator xpi = xpn.Select("/System[*]/Case[*]");
OR
XPathNodeIterator xpi = xpn.Select("/System[@*]/Case[@*]");

but to no avail. I cannot work out why one file can be read whilst the other cant. The only difference being is the attributes. It may well be a simple answer but after 2 days staring at this and scouring the internet I cannot fathom it out. Any help will be appreciated.

Using C# Visual Studio Express 2008 on a Windows XP Machine.

Regards

WestyCHC
Apr 14 '08 #1
1 2652
weaknessforcats
9,208 Expert Mod 8TB
I am moving this to the .NET forum.
Apr 14 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Bruce Dunwiddie | last post: by
3 posts views Thread by Brad Rogers | last post: by
reply views Thread by Ismail | last post: by
2 posts views Thread by =?Utf-8?B?Tm9yZW1hYw==?= | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.