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

How parse XML values from a string?

I have a string (see below) that I want to parse out the values. As you can
see, some are element-based and some are attribute-based.

<METADATA version="Format5"><TITLE value="Adrenaline
Rush"/><DESCRIPTION>Take a thrilling look at the world of skydiving and base
jumping - parachuting from a building, a bridge or a cliff. With
breathtaking views of skydiving over the Florida Keys, the Mojave Desert and
the magnificent Fjords of Norway, this giant-screen experience explores the
psychological and physiological forces behind risk-tasking and the physics
involved in high-risk activities.</DESCRIPTION><BARCODE
value="014381203820"/><BARCODE value="400002716808"/><PREVIEWIMAGE
value="IME120382C.png"/><MEDIATYPE value="DVD"/></METADATA>

Can someone please show me what the code would look like to do this?

Thanks,
Ron

P.S. Note that no nodes will be repeated (so I'm guessing it should be
simpler). -thx
Feb 12 '07 #1
1 5316
Ronald S. Cook wrote:
I have a string (see below) that I want to parse out the values. As you can
see, some are element-based and some are attribute-based.

<METADATA version="Format5"><TITLE value="Adrenaline
Rush"/><DESCRIPTION>Take a thrilling look at the world of skydiving and base
jumping - parachuting from a building, a bridge or a cliff. With
breathtaking views of skydiving over the Florida Keys, the Mojave Desert and
the magnificent Fjords of Norway, this giant-screen experience explores the
psychological and physiological forces behind risk-tasking and the physics
involved in high-risk activities.</DESCRIPTION><BARCODE
value="014381203820"/><BARCODE value="400002716808"/><PREVIEWIMAGE
value="IME120382C.png"/><MEDIATYPE value="DVD"/></METADATA>

Can someone please show me what the code would look like to do this?
Here is C# .NET 2.0 code that uses XmlReader to pull in nodes, if an
element has attributes if outputs the attributes, if there are no
attributes it reads out the element contents:

using (XmlReader xmlReader = XmlReader.Create(@"file.xml"))
{
while (xmlReader.Read())
{
if (xmlReader.NodeType == XmlNodeType.Element)
{
Console.WriteLine("Reading element {0}:",
xmlReader.Name);
if (xmlReader.HasAttributes)
{
while (xmlReader.MoveToNextAttribute())
{
Console.WriteLine("Attribute {0}={1}.",
xmlReader.Name, xmlReader.Value);
}
}
else
{
Console.WriteLine(
"Element content: \"{0}\".", xmlReader.ReadElementContentAsString());
}
Console.WriteLine();
}
}
}

..NET has various other ways (DOM, XPath, XSLT) to parse and extract XML
data so depending on your needs other ways might be more comfortable
than using XmlReader.
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Feb 12 '07 #2

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

Similar topics

2
by: N | last post by:
Hi, I would like to parse out each value that is seperated by a comma in a field and use that value to join to another table. What would be the easiest way to do so without having to write a...
22
by: Ram Laxman | last post by:
Hi all, I have a text file which have data in CSV format. "empno","phonenumber","wardnumber" 12345,2234353,1000202 12326,2243653,1000098 Iam a beginner of C/C++ programming. I don't know how to...
3
by: Ken Bush | last post by:
How can I write an update query that removes part of a field? Like if I have a field with values such as 8/3/68 (a birthday obviously) and I need to put values in a new column but I need...
5
by: tony collier | last post by:
I have ..... enum day {monday, tuesday, wednesday}; myArray=5; i=(int)Enum.Parse(typeof(day), wednesday);
5
by: Navid Azimi | last post by:
What's the best way to parse a currency string to a decimal given the possibility of multiple currencies? That is, my numeric string can be ($12.00) or -£12.00; in either case I want -12.00 to be...
9
by: Python.LeoJay | last post by:
Dear all, i need to parse billions of numbers from a file into float numbers for further calculation. i'm not satisfied with the speed of atof() function on my machine(i'm using visual c++ 6)....
5
by: BMeyer | last post by:
I have been losing my mind trying to parse an XML document (with nested child elements, not all of which appear in each parent node) into a DataGrid object. What I want to do is "flatten" the XML...
6
by: trevor | last post by:
Incorrect values when using float.Parse(string) I have discovered a problem with float.Parse(string) not getting values exactly correct in some circumstances(CSV file source) but in very similar...
1
AdrianH
by: AdrianH | last post by:
Assumptions I am assuming that you know or are capable of looking up the functions I am to describe here and have some remedial understanding of C programming. FYI Although I have called this...
2
by: shapper | last post by:
Hello, I have the following class ( Level is just a simple enumeration ): public class Theme { public Subject Subject { get; set; } public List<LevelLevels { get; set; } public string Note {...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.