473,407 Members | 2,315 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,407 software developers and data experts.

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 2802
weaknessforcats
9,208 Expert Mod 8TB
I am moving this to the .NET forum.
Apr 14 '08 #2

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

Similar topics

1
by: Phil | last post by:
Is there an easy way to get the schema's data type for a XPathNavigator value? I need to be able to determine the data type for the xml value based off of an unknown schema and unknown xml file...
1
by: Bruce Dunwiddie | last post by:
I'm trying to build a couple classes that would allow for writing xsl transforms against data that is not originally xml. I've got an xmlreader implementation that seems to work well. Based on some...
3
by: Brad Rogers | last post by:
All, Being immersed in vb.net and trying CSharp after almost a year I forgot the differences. I like vb fixing the uppercase/lowercase names and seeming to be more flexible to code entry. ...
1
by: SteZgr | last post by:
I have implemented in .NET 2.0 a custom XPathNavigator for Xpath filtering on an object tree. So far it works. While analyzing the performance, I have noticed a lot of MoveToNextAttribute calls. ...
11
by: ericms | last post by:
Can anybody show me how to insert a CDATA section using XPathNavigator ? I have tried the follwing with no luck: XmlDocument docNav = new XmlDocument(); docNav.LoadXml(xmlString);...
1
by: Keith Patrick | last post by:
I'm converting over from using XmlNodes (specifically ConfigXmlNode, but the API uses XmlNode) to IXPathNavigable in some methods I have, but I'm finding in the process, that I can't seem to get...
0
by: Ismail | last post by:
Guys, I have the following code in page_load event of user control Go.Attributes.Add("onclick","if(" + search.ClientID + ".value==''){alert('" + noSearchText + "');return false;}"); ...
2
by: =?Utf-8?B?Tm9yZW1hYw==?= | last post by:
Hi. Using VS2005, .NET 2.0. I have an xml document that I want to go through and set the values on attributes of elements. The elements are complex types defined in my schema (xsd) files. ...
0
by: WestyCHC | last post by:
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.