How parse XML values from a string?

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
 
P

Patrick Steele

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.
 
M

Morten Wennevik [C# MVP]

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;
}
 
O

Ollie Riches

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
 
Top