kasdoffe Posted January 17, 2006 Posted January 17, 2006 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? Quote
Cags Posted January 17, 2006 Posted January 17, 2006 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. Quote Anybody looking for a graduate programmer (Midlands, England)?
kasdoffe Posted January 17, 2006 Author Posted January 17, 2006 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. Quote
Cags Posted January 17, 2006 Posted January 17, 2006 Well as I said in my original post, theres also an overload that takes a string, try looking at that. Quote Anybody looking for a graduate programmer (Midlands, England)?
kasdoffe Posted January 17, 2006 Author Posted January 17, 2006 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. Quote
Mister E Posted January 18, 2006 Posted January 18, 2006 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. Quote
robplatt Posted September 27, 2006 Posted September 27, 2006 string = string.substring(string.substring.indexof("<Last>"),string.substring.indexof("</Last>")) less code... Quote
Administrators PlausiblyDamp Posted September 27, 2006 Administrators Posted September 27, 2006 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? Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.