Jump to content
Xtreme .Net Talk

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


Recommended Posts

Posted

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 wouldn't have to use string manipulation to find the location and parse the text.

 

Changing the format of the string to parse isn't 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?

Posted

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

 

		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.

Anybody looking for a graduate programmer (Midlands, England)?
Posted
Assuming you were getting that info from a file you could use...

 

		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, I'm not reading it from a file. I'm reading it from a socket. The socket sends up a bunch of data, some of it's xml, some of it's not. Take a look at my second string example in my original post.

Posted
Well as I said in my original post, theres also an overload that takes a string, try looking at that.
Anybody looking for a graduate programmer (Midlands, England)?
Posted
Well as I said in my original post' date=' theres also an overload that takes a string, try looking at that.[/quote']

 

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.

Posted
You could just load the XML string into an XmlDocument instance:
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.

  • 8 months later...
  • Administrators
Posted

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?

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...