Parse string like <First>John</First><Last>Doe</Last>

kasdoffe

Well-known member
Joined
Aug 27, 2003
Messages
57
How can I easily parse a string like the following?

<First>John</First><Last>Doe</Last><MiddleInitial>J</MiddleInitial>

If I want to extract the last name Doe from that string, is there an xml command to do so? I hope I wouldnt have to use string manipulation to find the location and parse the text.

Changing the format of the string to parse isnt an option. In fact, it could be even more messed up, like this:

156^first:1:3^last:2:4^<First>John</First><Last>Doe</Last><MiddleInitial>J</MiddleInitial>^blahblahblah

Any help?
 
Assuming you were getting that info from a file you could use...

C#:
			string path = ""; // path of file
			string name = "";

			System.Xml.XmlTextReader myReader = new System.Xml.XmlTextReader(path);
			
			while(myReader.Read())
				if(myReader.NodeType == System.Xml.XmlNodeType.Element)
					if(myReader.Name == "Last")
					{
						name = myReader.ReadInnerXml();
						break;
					}

Theres also an overload for passing in an xmlFragment as a string which might be better for you.

NB. Code not tested.
 
Cags said:
Assuming you were getting that info from a file you could use...

C#:
			string path = ""; // path of file
			string name = "";

			System.Xml.XmlTextReader myReader = new System.Xml.XmlTextReader(path);
			
			while(myReader.Read())
				if(myReader.NodeType == System.Xml.XmlNodeType.Element)
					if(myReader.Name == "Last")
					{
						name = myReader.ReadInnerXml();
						break;
					}

Theres also an overload for passing in an xmlFragment as a string which might be better for you.

NB. Code not tested.

No, Im not reading it from a file. Im reading it from a socket. The socket sends up a bunch of data, some of its xml, some of its not. Take a look at my second string example in my original post.
 
Cags said:
Well as I said in my original post, theres also an overload that takes a string, try looking at that.

i tried the xmlFragment override for xmlTextReader and it was able to move through the elements. However, I was hoping for a way to just grab a single element value rather than read until i get to the element, then grab the value.
 
You could just load the XML string into an XmlDocument instance:
C#:
string xml = "<Root><First>John</First><Last>Doe</Last><MiddleInitial>J</MiddleInitial></Root>";

System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.LoadXml(xml);

System.Xml.XmlNode node = doc.SelectSingleNode("//Root/First");

if ( node != null )
    Console.WriteLine(node.InnerText);
Note: that the Xml must be enclosed in a valid root.
 
string = string.substring(string.substring.indexof("<Last>"),string.substring.indexof("</Last>"))

less code...
 
Also less handling of escaped character sequences and other XML constructs if the string is actually valid XML.

Although on a tangent - I really do struggle to understand why people mix XML (or XML-like) data in with another data structure. Why have an already existing format like a : or ^ delimited string and then embed within that a completely different structure?
 
Back
Top