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
- XPathDocument xpd = new XPathDocument(ReqFile);
- XPathNavigator xpn = xpd.CreateNavigator();
- XPathNodeIterator xpi = xpn.Select("/System/Case");
- Console.WriteLine("0, Current name = " + xpi.Current.Name);
- while (xpi.MoveNext()) // each testcase node
- {
- Console.WriteLine("1, Current name = " + xpi.Current.Name);
- XPathNodeIterator tcChild = xpi.Current.SelectChildren(XPathNodeType.Element);
- while (tcChild.MoveNext()) // each part (<inputs> and <expected>) of <testcase>
- {
- Console.WriteLine("2, tcChildName value = " + tcChild.Current.Value + ", tcChildName = " + tcChild.Current.Name);
This will read and therefore write to the output window the following XML file:
[XML]
<?xml version="1.0" encoding="utf-8"?>[/XML]
<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>
but it wont read:
[XML]
<?xml version="1.0" encoding="utf-8"?>[/XML]
<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>
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