Solved

Reading XML files by name in C#

Posted on 2007-04-03
3
255 Views
Last Modified: 2008-02-01
Is there a way in C# to read nodes of XML by the node name?  I want to be able to read the following:

http://www.markit.com/news/RCD_ABX_HE_INDEX.xml

I want to extract the <red></red> code for each item and then within the <currentsettlement> I want to get the <factor>.

Thakns
0
Comment
Question by:techhound
3 Comments
 
LVL 3

Accepted Solution

by:
emce earned 300 total points
ID: 18843984
XPathDocument xpd   = new XPathDocument( RCD_ABX_HE_INDEX.xml );                                          
XPathNavigator xpn  = xpd.CreateNavigator();
XPathNodeIterator i = xpn.Select("//data/abcds/red");
if ( i.MoveNext() )
  red = i.Current.Value;
xpn.Select("//data/abcds/currentsettlement/factot");
if ( i.MoveNext() )
  factor = i.Current.Value;
0
 

Author Comment

by:techhound
ID: 18844079
Hi,

The red code is correct but the factor gives me the red code as well.  I did change the misspelling from your code segment to the correct spelling, i.e., factot => factor.

Thanks
0
 
LVL 5

Assisted Solution

by:mzalewski
mzalewski earned 200 total points
ID: 18847508
Not sure if this will help, but try this:

XPathDocument xpd   = new XPathDocument( RCD_ABX_HE_INDEX.xml );                                          
XPathNavigator xpn  = xpd.CreateNavigator();
XPathNodeIterator i = xpn.Select("//data/abcds/red");
if ( i.MoveNext() )
  red = i.Current.Value;
i = xpn.Select("//data/abcds/currentsettlement/factor");
if ( i.MoveNext() )
  factor = i.Current.Value;

I think emce might have forgotten to assign the returned XPathNodeIterator to 'i' (the second xpn.Select).
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question