How parse XML values from a string?

  • Thread starter Thread starter Ronald S. Cook
  • Start date Start date
R

Ronald S. Cook

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
 
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?

Look into XmlTextReader and StringReader. They'll get you going.
 
Hi Ronald

You can parse this easily using XPath queries. Load the xml into an
XmlDocument and use SelectSingleNode/SelectNodes to get a reference to the
various nodes. The code below will work for unlimited METADATA nodes as
well as BARCODE nodes.

string data =
@"<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>";

XmlDocument doc = new XmlDocument();
doc.LoadXml(data);

string s = "";

XmlNodeList list = doc.SelectNodes("METADATA");
foreach (XmlNode node in list)
{
s += "Title: " + node["TITLE"].Attributes["value"].Value
+ Environment.NewLine;
s += "Description: " + node["DESCRIPTION"].InnerText
+ Environment.NewLine;

XmlNodeList barList = node.SelectNodes("BARCODE");
foreach (XmlNode barNode in barList)
{
s += "Barcode: " + barNode.Attributes["value"].Value
+ Environment.NewLine;
}

s += "PreviewImage: " + node["PREVIEWIMAGE"].Attributes["value"].Value
+ Environment.NewLine;
s += "Mediatype: " + node["MEDIATYPE"].Attributes["value"].Value
+ Environment.NewLine;
}
 
if the xml schema is known, why not reverse this into .Net class using the
xsd.exe tool and then use xml serialisation at rutime to manipulate the xml,
this is far easier and quicker than deailing with xml directly.

HTH

Ollie Riches
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top