Parse a tagged value string

  • Thread starter Thread starter Steven Blair
  • Start date Start date
S

Steven Blair

I have the following string:

<CardDetails>4561</CardDetails><Message Identifier>1</Message
Identifier><TID>88888888</TID>

I need to be able to extract the values from the tags easily in C#.
Being able to search through this string at any point for a partiuclar
tag would be useful.

Can anyone suggest the best / most effecient way to achieve this?

Can any XML classes be used, or any other libraries?

Thanks for the help.

Steven
 
Is the above string contained in a complete xml file? If so have a
look at the xmlserializer routines. You can create and load a class
based on the details in that class.
 
Files are not used in this application.
I received a string object from another process and must extract tag
values.

I think I got soemthing working, but not sure if its the best approach.
Comments welcome:

(It uses XmlDocument)

http://pastebin.com/709199

Here is the complete listing for future reference:

XmlDocument doc = new XmlDocument();
xmlString = "<?xml version='1.0'
encoding='utf-8'?><Transaction><CardDetails>4561</CardDetails><MessageTy
pe>1</MessageType><TID>88888888</TID></Transaction>";

doc.LoadXml(xmlString);
XmlNodeList elemList = doc.GetElementsByTagName("MessageType");
string test = elemList[0].InnerXml;
 
You might also look at the XPath classes. These are great for ferreting
stuff out of an XML file.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
Back
Top