curiouswebster
asked on
Using XPath to read XML data
I need to read an XML file and retrieve a list it contains, then iterate through it.
Can I do this with XPath?
Could someone show me how?
thanks,
newbieweb
Can I do this with XPath?
Could someone show me how?
thanks,
newbieweb
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
HooKooDooKu,
Could you do this in C#?
newbieweb
Could you do this in C#?
newbieweb
I'm not a C# programmer, but by pulling up a C# editor, it looks like the following code would be functionally equivilent.
using System.Xml //Add Xml capability to the code
...
XmlDocument Doc;
Doc.Load( FileName );
XmlElement RootNode = Doc.DocumentElement;
String XPathString = "What ever XPath Query you Need";
XmlNodeList NodeList = RootNode.SelectNodes( XPathString );
for( int i = 0; i < NodeList.Count; i++ )
{
XmlNode Node = NodeList.Item(i);
//Do Stuff with Node Here
}
ASKER
OK. Now I have the C# code compiling and finding the document okay. But I'm not sure what to use for XPathString.
I need to get all records where
RecordLocator = "YVRSEA"
and need to extract FName and LName for each Passenger record.
How do i do this?
newbieweb
private void PopulatePage()
{
XmlDocument Doc = new XmlDocument();
Doc.Load( @"C:\MyWork\Test\BlankASPT est\testda ta\reserva tion.xml") ;
XmlElement RootNode = Doc.DocumentElement;
String XPathString = "What ever XPath Query you Need";
XmlNodeList NodeList = RootNode.SelectNodes( XPathString );
for( int i = 0; i < NodeList.Count; i++ )
{
XmlNode Node = NodeList.Item(i);
//Do Stuff with Node Here
}
}
I need to get all records where
RecordLocator = "YVRSEA"
and need to extract FName and LName for each Passenger record.
How do i do this?
newbieweb
private void PopulatePage()
{
XmlDocument Doc = new XmlDocument();
Doc.Load( @"C:\MyWork\Test\BlankASPT
XmlElement RootNode = Doc.DocumentElement;
String XPathString = "What ever XPath Query you Need";
XmlNodeList NodeList = RootNode.SelectNodes( XPathString );
for( int i = 0; i < NodeList.Count; i++ )
{
XmlNode Node = NodeList.Item(i);
//Do Stuff with Node Here
}
}
What does the XML look like (sort of hard to build a database query without knowing the structure of the database)?
ASKER
<?xml version="1.0" encoding="UTF-8" ?>
<FindResByLocatorResponse>
<TransactionID>TEST-031219 161726</Tr ansactionI D>
<PNR>
<RecordLocator>YVRSEA</Rec ordLocator >
<Type>0</Type>
<Header>Regular</Header>
<Passenger>
<LName>METHENY</LName>
<FName>PAT</FName>
<Type>ADT</Type>
<PassengerNumber>1.1</Pass engerNumbe r>
<FFN>
<Number>098745341</Number>
<Type>AS</Type>
<AwardLevel>1</AwardLevel>
</FFN>
<ExistingBagCount>0</Exist ingBagCoun t>
<MaxFreeBagCount>2</MaxFre eBagCount>
<StartPassenger/>
<Seat>
<Number>40-C</Number>
</Seat>
<CouponBook>
<FName>PAT</FName>
<LName>METHENY</LName>
<Coupon>
<Carrier>NW</Carrier>
<FlightNumber>711</FlightN umber>
<DeparturePoint>MCO</Depar turePoint>
<ArrivalPoint>DTW</Arrival Point>
<CouponNumber>01</CouponNu mber>
<Date>19DEC03</Date>
<DepartureTime>2055</Depar tureTime>
<Class>Q</Class>
<FareBasis>QEP21NR1</FareB asis>
<ActualStatus>O</ActualSta tus>
<SegmentNumber>01</Segment Number>
<Status>OK</Status>
<Valid/>
<DepartureCity>Orlando</De partureCit y>
<ArrivalCity>Detroit</Arri valCity>
</Coupon>
<Coupon>
<Carrier>NW</Carrier>
<FlightNumber>778</FlightN umber>
<DeparturePoint>DTW</Depar turePoint>
<ArrivalPoint>MCO</Arrival Point>
<CouponNumber>02</CouponNu mber>
<Date>30DEC03</Date>
<DepartureTime>0700</Depar tureTime>
<Class>K</Class>
<FareBasis>KEO21NR1</FareB asis>
<ActualStatus>O</ActualSta tus>
<Status>OK</Status>
<DepartureCity>Detroit</De partureCit y>
<ArrivalCity>Orlando</Arri valCity>
</Coupon>
<TicketNumber>012157261412 0 </TicketNumber>
<DateOfIssue>23SEP03</Date OfIssue>
<PlaceOfIssue>NORTHWEST AIRLINES/ORBITZ MPLS/ST PAUL MN </PlaceOfIssue>
<FormOfPayment>VI</FormOfP ayment>
<FOPType>CC</FOPType>
<Fare>
<Base> 257.68</Base>
<Total> 290.50</Total>
<Currency>USD</Currency>
<Tax>
<Value>US</Value>
<Type> 19.32</Type>
</Tax>
<Tax>
<Value>ZP</Value>
<Type> 6.00</Type>
</Tax>
<Tax>
<Value>XF</Value>
<Type> 7.50</Type>
</Tax>
<Calculation></Calculation >
<CalculationWithTaxes></Ca lculationW ithTaxes>
</Fare>
<EndorsementRestrictions>N ON-REFUNDA BLE//PENAL TY FOR CHANGES </EndorsementRestrictions>
</CouponBook>
<NameFieldStatus></NameFie ldStatus>
</Passenger>
<Passenger>
<LName>SCOFIELD</LName>
<FName>JOHN</FName>
<Type>ADT</Type>
<PassengerNumber>2.1</Pass engerNumbe r>
<FFN>
<Number>880932850434</Numb er>
<Type>CO</Type>
<AwardLevel>1</AwardLevel>
</FFN>
<ExistingBagCount>0</Exist ingBagCoun t>
<MaxFreeBagCount>2</MaxFre eBagCount>
<Seat>
<Number>40-D</Number>
</Seat>
<CouponBook>
<FName>JOHN</FName>
<LName>SCOFIELD</LName>
<Coupon>
<Carrier>NW</Carrier>
<FlightNumber>711</FlightN umber>
<DeparturePoint>MCO</Depar turePoint>
<ArrivalPoint>DTW</Arrival Point>
<CouponNumber>01</CouponNu mber>
<Date>19DEC03</Date>
<DepartureTime>2055</Depar tureTime>
<Class>Q</Class>
<FareBasis>QEP21NR1</FareB asis>
<ActualStatus>O</ActualSta tus>
<SegmentNumber>01</Segment Number>
<Status>OK</Status>
<Valid/>
<DepartureCity>Orlando</De partureCit y>
<ArrivalCity>Detroit</Arri valCity>
</Coupon>
<Coupon>
<Carrier>NW</Carrier>
<FlightNumber>778</FlightN umber>
<DeparturePoint>DTW</Depar turePoint>
<ArrivalPoint>MCO</Arrival Point>
<CouponNumber>02</CouponNu mber>
<Date>30DEC03</Date>
<DepartureTime>0700</Depar tureTime>
<Class>K</Class>
<FareBasis>KEO21NR1</FareB asis>
<ActualStatus>O</ActualSta tus>
<Status>OK</Status>
<DepartureCity>Detroit</De partureCit y>
<ArrivalCity>Orlando</Arri valCity>
</Coupon>
<TicketNumber>012157261412 0 </TicketNumber>
<DateOfIssue>23SEP03</Date OfIssue>
<PlaceOfIssue>NORTHWEST AIRLINES/ORBITZ MPLS/ST PAUL MN </PlaceOfIssue>
<FormOfPayment>VI</FormOfP ayment>
<FOPType>CC</FOPType>
<Fare>
<Base> 257.68</Base>
<Total> 290.50</Total>
<Currency>USD</Currency>
<Tax>
<Value>US</Value>
<Type> 19.32</Type>
</Tax>
<Tax>
<Value>ZP</Value>
<Type> 6.00</Type>
</Tax>
<Tax>
<Value>XF</Value>
<Type> 7.50</Type>
</Tax>
<Calculation></Calculation >
<CalculationWithTaxes></Ca lculationW ithTaxes>
</Fare>
<EndorsementRestrictions>N ON-REFUNDA BLE//PENAL TY FOR CHANGES </EndorsementRestrictions>
</CouponBook>
<NameFieldStatus></NameFie ldStatus>
</Passenger>
<Passenger>
<LName>COLTRANE</LName>
<FName>JOHN</FName>
<Type>ADT</Type>
<PassengerNumber>2.1</Pass engerNumbe r>
<FFN>
<Number>338847119</Number>
<Type>NW</Type>
<AwardLevel>1</AwardLevel>
</FFN>
<ExistingBagCount>0</Exist ingBagCoun t>
<MaxFreeBagCount>2</MaxFre eBagCount>
<Seat>
<Number>40-E</Number>
</Seat>
<CouponBook>
<FName>JOHN</FName>
<LName>COLTRANE</LName>
<Coupon>
<Carrier>NW</Carrier>
<FlightNumber>711</FlightN umber>
<DeparturePoint>MCO</Depar turePoint>
<ArrivalPoint>DTW</Arrival Point>
<CouponNumber>01</CouponNu mber>
<Date>19DEC03</Date>
<DepartureTime>2055</Depar tureTime>
<Class>Q</Class>
<FareBasis>QEP21NR1</FareB asis>
<ActualStatus>O</ActualSta tus>
<SegmentNumber>01</Segment Number>
<Status>OK</Status>
<Valid/>
<DepartureCity>Orlando</De partureCit y>
<ArrivalCity>Detroit</Arri valCity>
</Coupon>
<Coupon>
<Carrier>NW</Carrier>
<FlightNumber>778</FlightN umber>
<DeparturePoint>DTW</Depar turePoint>
<ArrivalPoint>MCO</Arrival Point>
<CouponNumber>02</CouponNu mber>
<Date>30DEC03</Date>
<DepartureTime>0700</Depar tureTime>
<Class>K</Class>
<FareBasis>KEO21NR1</FareB asis>
<ActualStatus>O</ActualSta tus>
<Status>OK</Status>
<DepartureCity>Detroit</De partureCit y>
<ArrivalCity>Orlando</Arri valCity>
</Coupon>
<TicketNumber>012157261412 0 </TicketNumber>
<DateOfIssue>23SEP03</Date OfIssue>
<PlaceOfIssue>NORTHWEST AIRLINES/ORBITZ MPLS/ST PAUL MN </PlaceOfIssue>
<FormOfPayment>VI</FormOfP ayment>
<FOPType>CC</FOPType>
<Fare>
<Base> 257.68</Base>
<Total> 290.50</Total>
<Currency>USD</Currency>
<Tax>
<Value>US</Value>
<Type> 19.32</Type>
</Tax>
<Tax>
<Value>ZP</Value>
<Type> 6.00</Type>
</Tax>
<Tax>
<Value>XF</Value>
<Type> 7.50</Type>
</Tax>
<Calculation></Calculation >
<CalculationWithTaxes></Ca lculationW ithTaxes>
</Fare>
<EndorsementRestrictions>N ON-REFUNDA BLE//PENAL TY FOR CHANGES </EndorsementRestrictions>
</CouponBook>
<NameFieldStatus></NameFie ldStatus>
</Passenger>
<Segment>
<Type>0</Type>
<Status>HK</Status>
<FlightNumber>0711</Flight Number>
<DeparturePoint>MCO</Depar turePoint>
<ArrivalPoint>DTW</Arrival Point>
<DepartureDate>20MAY04</De partureDat e>
<DepartureTime>1025</Depar tureTime>
<ArrivalDate>20MAY04</Arri valDate>
<ArrivalTime>1243</Arrival Time>
<Class>Q</Class>
<Index>01</Index>
<StopoverCity></StopoverCi ty>
<BucketTypes></BucketTypes >
<BoardTime>2055</BoardTime >
<Carrier>NW</Carrier>
<DepartureGate>G14</Depart ureGate>
<ArrivalGate>D14</ArrivalG ate>
<EquipmentCode>755</Equipm entCode>
<FlightStatus>ON TIME</FlightStatus>
<DepartureCity>Orlando</De partureCit y>
<ArrivalCity>Atlanta</Arri valCity>
<UpgradeStatus>0</UpgradeS tatus>
<SelectSeatEligible/>
<ScheduleChange/>
</Segment>
<SeatsAutoAssigned/>
</PNR>
<Result>1</Result>
</FindResByLocatorResponse >
<FindResByLocatorResponse>
<TransactionID>TEST-031219
<PNR>
<RecordLocator>YVRSEA</Rec
<Type>0</Type>
<Header>Regular</Header>
<Passenger>
<LName>METHENY</LName>
<FName>PAT</FName>
<Type>ADT</Type>
<PassengerNumber>1.1</Pass
<FFN>
<Number>098745341</Number>
<Type>AS</Type>
<AwardLevel>1</AwardLevel>
</FFN>
<ExistingBagCount>0</Exist
<MaxFreeBagCount>2</MaxFre
<StartPassenger/>
<Seat>
<Number>40-C</Number>
</Seat>
<CouponBook>
<FName>PAT</FName>
<LName>METHENY</LName>
<Coupon>
<Carrier>NW</Carrier>
<FlightNumber>711</FlightN
<DeparturePoint>MCO</Depar
<ArrivalPoint>DTW</Arrival
<CouponNumber>01</CouponNu
<Date>19DEC03</Date>
<DepartureTime>2055</Depar
<Class>Q</Class>
<FareBasis>QEP21NR1</FareB
<ActualStatus>O</ActualSta
<SegmentNumber>01</Segment
<Status>OK</Status>
<Valid/>
<DepartureCity>Orlando</De
<ArrivalCity>Detroit</Arri
</Coupon>
<Coupon>
<Carrier>NW</Carrier>
<FlightNumber>778</FlightN
<DeparturePoint>DTW</Depar
<ArrivalPoint>MCO</Arrival
<CouponNumber>02</CouponNu
<Date>30DEC03</Date>
<DepartureTime>0700</Depar
<Class>K</Class>
<FareBasis>KEO21NR1</FareB
<ActualStatus>O</ActualSta
<Status>OK</Status>
<DepartureCity>Detroit</De
<ArrivalCity>Orlando</Arri
</Coupon>
<TicketNumber>012157261412
<DateOfIssue>23SEP03</Date
<PlaceOfIssue>NORTHWEST AIRLINES/ORBITZ MPLS/ST PAUL MN </PlaceOfIssue>
<FormOfPayment>VI</FormOfP
<FOPType>CC</FOPType>
<Fare>
<Base> 257.68</Base>
<Total> 290.50</Total>
<Currency>USD</Currency>
<Tax>
<Value>US</Value>
<Type> 19.32</Type>
</Tax>
<Tax>
<Value>ZP</Value>
<Type> 6.00</Type>
</Tax>
<Tax>
<Value>XF</Value>
<Type> 7.50</Type>
</Tax>
<Calculation></Calculation
<CalculationWithTaxes></Ca
</Fare>
<EndorsementRestrictions>N
</CouponBook>
<NameFieldStatus></NameFie
</Passenger>
<Passenger>
<LName>SCOFIELD</LName>
<FName>JOHN</FName>
<Type>ADT</Type>
<PassengerNumber>2.1</Pass
<FFN>
<Number>880932850434</Numb
<Type>CO</Type>
<AwardLevel>1</AwardLevel>
</FFN>
<ExistingBagCount>0</Exist
<MaxFreeBagCount>2</MaxFre
<Seat>
<Number>40-D</Number>
</Seat>
<CouponBook>
<FName>JOHN</FName>
<LName>SCOFIELD</LName>
<Coupon>
<Carrier>NW</Carrier>
<FlightNumber>711</FlightN
<DeparturePoint>MCO</Depar
<ArrivalPoint>DTW</Arrival
<CouponNumber>01</CouponNu
<Date>19DEC03</Date>
<DepartureTime>2055</Depar
<Class>Q</Class>
<FareBasis>QEP21NR1</FareB
<ActualStatus>O</ActualSta
<SegmentNumber>01</Segment
<Status>OK</Status>
<Valid/>
<DepartureCity>Orlando</De
<ArrivalCity>Detroit</Arri
</Coupon>
<Coupon>
<Carrier>NW</Carrier>
<FlightNumber>778</FlightN
<DeparturePoint>DTW</Depar
<ArrivalPoint>MCO</Arrival
<CouponNumber>02</CouponNu
<Date>30DEC03</Date>
<DepartureTime>0700</Depar
<Class>K</Class>
<FareBasis>KEO21NR1</FareB
<ActualStatus>O</ActualSta
<Status>OK</Status>
<DepartureCity>Detroit</De
<ArrivalCity>Orlando</Arri
</Coupon>
<TicketNumber>012157261412
<DateOfIssue>23SEP03</Date
<PlaceOfIssue>NORTHWEST AIRLINES/ORBITZ MPLS/ST PAUL MN </PlaceOfIssue>
<FormOfPayment>VI</FormOfP
<FOPType>CC</FOPType>
<Fare>
<Base> 257.68</Base>
<Total> 290.50</Total>
<Currency>USD</Currency>
<Tax>
<Value>US</Value>
<Type> 19.32</Type>
</Tax>
<Tax>
<Value>ZP</Value>
<Type> 6.00</Type>
</Tax>
<Tax>
<Value>XF</Value>
<Type> 7.50</Type>
</Tax>
<Calculation></Calculation
<CalculationWithTaxes></Ca
</Fare>
<EndorsementRestrictions>N
</CouponBook>
<NameFieldStatus></NameFie
</Passenger>
<Passenger>
<LName>COLTRANE</LName>
<FName>JOHN</FName>
<Type>ADT</Type>
<PassengerNumber>2.1</Pass
<FFN>
<Number>338847119</Number>
<Type>NW</Type>
<AwardLevel>1</AwardLevel>
</FFN>
<ExistingBagCount>0</Exist
<MaxFreeBagCount>2</MaxFre
<Seat>
<Number>40-E</Number>
</Seat>
<CouponBook>
<FName>JOHN</FName>
<LName>COLTRANE</LName>
<Coupon>
<Carrier>NW</Carrier>
<FlightNumber>711</FlightN
<DeparturePoint>MCO</Depar
<ArrivalPoint>DTW</Arrival
<CouponNumber>01</CouponNu
<Date>19DEC03</Date>
<DepartureTime>2055</Depar
<Class>Q</Class>
<FareBasis>QEP21NR1</FareB
<ActualStatus>O</ActualSta
<SegmentNumber>01</Segment
<Status>OK</Status>
<Valid/>
<DepartureCity>Orlando</De
<ArrivalCity>Detroit</Arri
</Coupon>
<Coupon>
<Carrier>NW</Carrier>
<FlightNumber>778</FlightN
<DeparturePoint>DTW</Depar
<ArrivalPoint>MCO</Arrival
<CouponNumber>02</CouponNu
<Date>30DEC03</Date>
<DepartureTime>0700</Depar
<Class>K</Class>
<FareBasis>KEO21NR1</FareB
<ActualStatus>O</ActualSta
<Status>OK</Status>
<DepartureCity>Detroit</De
<ArrivalCity>Orlando</Arri
</Coupon>
<TicketNumber>012157261412
<DateOfIssue>23SEP03</Date
<PlaceOfIssue>NORTHWEST AIRLINES/ORBITZ MPLS/ST PAUL MN </PlaceOfIssue>
<FormOfPayment>VI</FormOfP
<FOPType>CC</FOPType>
<Fare>
<Base> 257.68</Base>
<Total> 290.50</Total>
<Currency>USD</Currency>
<Tax>
<Value>US</Value>
<Type> 19.32</Type>
</Tax>
<Tax>
<Value>ZP</Value>
<Type> 6.00</Type>
</Tax>
<Tax>
<Value>XF</Value>
<Type> 7.50</Type>
</Tax>
<Calculation></Calculation
<CalculationWithTaxes></Ca
</Fare>
<EndorsementRestrictions>N
</CouponBook>
<NameFieldStatus></NameFie
</Passenger>
<Segment>
<Type>0</Type>
<Status>HK</Status>
<FlightNumber>0711</Flight
<DeparturePoint>MCO</Depar
<ArrivalPoint>DTW</Arrival
<DepartureDate>20MAY04</De
<DepartureTime>1025</Depar
<ArrivalDate>20MAY04</Arri
<ArrivalTime>1243</Arrival
<Class>Q</Class>
<Index>01</Index>
<StopoverCity></StopoverCi
<BucketTypes></BucketTypes
<BoardTime>2055</BoardTime
<Carrier>NW</Carrier>
<DepartureGate>G14</Depart
<ArrivalGate>D14</ArrivalG
<EquipmentCode>755</Equipm
<FlightStatus>ON TIME</FlightStatus>
<DepartureCity>Orlando</De
<ArrivalCity>Atlanta</Arri
<UpgradeStatus>0</UpgradeS
<SelectSeatEligible/>
<ScheduleChange/>
</Segment>
<SeatsAutoAssigned/>
</PNR>
<Result>1</Result>
</FindResByLocatorResponse
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
http://msdn.microsoft.com/en-us/library/ms256086.aspx
As to how to use it, I've used it using Microsoft's DOM (Document Object Model) of access to an XML file.
http://msdn.microsoft.com/en-us/library/ms753804(VS.85).aspx
When I was using XPath, Microsofts XML reader was at version 4, so the crude code snippet below (written in VB6) is using version 4 (msxml4.dll).
Open in new window