473,473 Members | 2,178 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

C# XPathNavigator doesnt like attributes and wont find the data

3 New Member
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 15 '08 #1
0 935

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...
12
by: David Thielen | last post by:
Hi; I have an element: <space> </space> When I call SelectSingleNode() on it, the InnerXml is a 0 length String, not a String containing 1 space. Any ideas?
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. ...
1
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.