473,385 Members | 1,693 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,385 software developers and data experts.

Good way to read this XML

Hi! I'm not yet that familliar with the Xml namespace, but have been
fooling around with GetElementsByTagName, ChildNodes and InnerText. I'm
wondering how others would approach extracting values out of this XML (see
below).

Basically, I need to extract all of the <code> values below as well as the
<status>, <transaction-id> and <time-stamp> values.

It seems to me that the most intuitive way of accomplishing this is by being
able to specify the path to each value I'm interested in. Something like
(warning: this is made up):

xmlDoc.Elements("message/record/vin/code").Value
xmlDoc.Elements("message/record/bill/address_current/code").Value

Is there a way to access the XML document this way?? If not, any other
suggestions are appreciated.

Thanks in advance!
Ben

<?xml version="1.0" encoding="UTF-8" ?>
<message>
<status>0</status>
<record>
<transaction-id>11</transaction-id>
<time-stamp>11/13/04 1:10 pm</time-stamp>
<vin>
<code>1</code>
<text>positive</text>
</vin>
<phone>
<code>1</code>
<text>positive</text>
</phone>
<bill>
<code>1</code>
<text>positive</text>
<address_current>
<code>1</code>
<text>positive</text>
</address_current>
</bill>
</record>
</message>
Nov 12 '05 #1
2 2093


Ben Amada wrote:

It seems to me that the most intuitive way of accomplishing this is by being
able to specify the path to each value I'm interested in. Something like
(warning: this is made up):

xmlDoc.Elements("message/record/vin/code").Value
xmlDoc.Elements("message/record/bill/address_current/code").Value

Is there a way to access the XML document this way?? If not, any other


What you have is an XPath expression and .NET allows you indeed to
select nodes with such an XPath expression. If all you want to do is
extracting data from an XML document then you can create an
XPathNavigator over an XPathDocument. Here is a C# snippet:

using System;
using System.Xml.XPath;

public class Test2004111401 {
public static void Main (string[] args) {
XPathDocument xmlDocument = new XPathDocument(@"test2004111401.xml");
XPathNavigator xpathNavigator = xmlDocument.CreateNavigator();

XPathNodeIterator nodeIterator =
xpathNavigator.Select(@"message/record/vin/code");
while (nodeIterator.MoveNext()) {
Console.WriteLine("vin code: {0}", nodeIterator.Current.Value);
}

nodeIterator =
xpathNavigator.Select(@"message/record/bill/address_current/code");
while (nodeIterator.MoveNext()) {
Console.WriteLine("adress_current code: {0}",
nodeIterator.Current.Value);
}
}
}
--

Martin Honnen
http://JavaScript.FAQTs.com/
Nov 12 '05 #2
Martin Honnen wrote:
What you have is an XPath expression and .NET allows you indeed to
select nodes with such an XPath expression. If all you want to do is
extracting data from an XML document then you can create an
XPathNavigator over an XPathDocument. Here is a C# snippet:

using System;
using System.Xml.XPath;

public class Test2004111401 {
public static void Main (string[] args) {
XPathDocument xmlDocument = new XPathDocument(@"test2004111401.xml");
XPathNavigator xpathNavigator = xmlDocument.CreateNavigator();

XPathNodeIterator nodeIterator =
xpathNavigator.Select(@"message/record/vin/code");
while (nodeIterator.MoveNext()) {
Console.WriteLine("vin code: {0}", nodeIterator.Current.Value);
}

nodeIterator =
xpathNavigator.Select(@"message/record/bill/address_current/code");
while (nodeIterator.MoveNext()) {
Console.WriteLine("adress_current code: {0}",
nodeIterator.Current.Value);
}
}
}


Hi Martin,

Great, your example has been extremely helpful! XPathNavigator works a lot
better than what I was trying to do with GetElementsByTagName, ChildNodes,
etc.

Thanks again!
Ben
Nov 12 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

24
by: matty | last post by:
Go away for a few days and you miss it all... A few opinions... Programming is a craft more than an art (software engineering, not black magic) and as such, is about writing code that works,...
3
by: Java script Dude | last post by:
I have still yet to see a JavaScript Editor that comes close to reading a good JS book, learing it and using it with a text editor. Anyway, here my recipe for build successfull DHTML...
59
by: seberino | last post by:
I've heard 2 people complain that word 'global' is confusing. Perhaps 'modulescope' or 'module' would be better? Am I the first peope to have thought of this and suggested it? Is this a...
12
by: G. | last post by:
Hi all, During my degree, BEng (Hons) Electronics and Communications Engineering, we did C programming every year, but I never kept it up, as I had no interest and didn't see the point. But now...
0
by: Daniel Sélen Secches | last post by:
I found a good class to do a simple FTP. Very good.... I'm posting it with the message, i hope it helps someone ============================================================== Imports...
15
by: Alex L Pavluck | last post by:
I am new to programming other than SAS. I read that C# is a good starting language and I have started to create some simple programs with C# 2005 express edition. Can someone let me know if this...
2
by: dotnet dude | last post by:
What boook do you guys recommend for somebody with the software development backgroud who wants to get started from scratch with the designing of good sofware application. I am interested in...
206
by: WaterWalk | last post by:
I've just read an article "Building Robust System" by Gerald Jay Sussman. The article is here: http://swiss.csail.mit.edu/classes/symbolic/spring07/readings/robust-systems.pdf In it there is a...
11
by: Diego Martins | last post by:
for me, these items are in the 'tricky zone' of C++ does anyone know good material with that? (dealing with subtle details, pitfalls, good practices...) anything like the Effective series from...
76
by: lorlarz | last post by:
Crockford's JavaScript, The Good Parts (a book review). This shall perhaps be the world's shortest book review (for one of the world's shortests books). I like Douglas Crockford (because I am a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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,...

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.