C# XPathNavigator doesnt like attributes and wont find the data

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • WestyCHC
    New Member
    • Apr 2008
    • 3

    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:
    Code:
                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"?>
    <System>
    <Control xmlns="http://www.webaddress-here.co.uk/mysystem-pos/1.0">
    <SystemVersion> 1.0</SystemVersion>
    </Control>
    <Case>
    <ReviewType />
    <AdviceBasisTyp e />
    <CreatedDate>20 08-04-07T15:11:39.747 </CreatedDate>
    <LastUpdatedDat e>2008-04-07T15:13:59</LastUpdatedDate >
    <Reference>XX-666</Reference>
    <CheckedStatu s />
    <StatusHistor y currentStatus=" AgendaSetComple te">
    <Status date="2008-04-07T15:11:40" type="AgendaSet Incomplete" />
    <Status date="2008-04-07T15:12:13" type="AgendaSet Complete" />
    </StatusHistory>
    <Notes />
    <AttitudeToRi sk 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="Adventuro us">Adventurous </ClientATR>
    <Notes />
    </AttitudeToRisk>
    <FactFindQuesti ons />
    <Parties>
    <HasDependant s 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.webaddr ess-here.co.uk/mysystem-pos/1.0" id="XY45E4D1-654B-4BF3-BB26-1647FFECA021">
    <ReviewType />
    <AdviceBasisTyp e />
    <CreatedDate>20 08-04-07T15:11:39.747 </CreatedDate>
    <LastUpdatedDat e>2008-04-14T09:45:54</LastUpdatedDate >
    <Reference>XX-666</Reference>
    <CheckedStatu s />
    <StatusHistor y currentStatus=" AgendaSetComple te">
    <Status date="2008-04-07T15:11:40" type="AgendaSet Incomplete" />
    <Status date="2008-04-07T15:12:13" type="AgendaSet Complete" />
    </StatusHistory>
    <Notes />
    <AttitudeToRi sk xmlns="http://www.webaddress-here.co.uk/mysystem-pos/1.0" xmlns:fa="http://www.www.webaddr ess-here.co.uk/mysystem-pos/1.0" questionnaireId ="2">
    <ClientATR code="Adventuro us">Adventurous </ClientATR>
    <Notes />
    </AttitudeToRisk>
    <FactFindQuesti ons />
    <Parties xmlns="http://www.www.webaddr ess-here.co.uk/mysystem-pos/1.0">
    <HasDependant s 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="Prospec t" 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:
    XPathNodeIterat or xpi = xpn.Select("/System[*]/Case[*]");
    OR
    XPathNodeIterat or 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
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    I am moving this to the .NET forum.

    Comment

    Working...