Close

XMLSerializer and XPath


I say working with smart people always pays off; For instance last week when I was encountered a particular web service response which wasn't getting parsed by usual XPATH query.


Dim child As XmlNode = doc.SelectSingleNode("/<node>")


As it later turned out, it was because there was no namespace prefix to it. XMLNamespace manager didn't help so Jeremy, our architect with honorary title of "King of RegEx and XPath" suggested using the following:


child.SelectSingleNode("node()[name() = '<node>']").InnerText.Equals(String.Empty)


which no need to mention, worked like charm. He also demonstrated string concatenation has the easiest overload for adding nodes to a DOM tree instead of doing an XMLNode instantiation exercise.


Also, there is gold in these framework class libraries. For instance the following XMLSerializer code snippet always comes in handy for (de)serialization.


Dim StringWriter As New System.IO.StringWriter
Dim XmlSerializer As New System.XML.Serialization.XmlSerializer(<responseObject>.GetType, String.Empty)XmlSerializer.Serialize(StringWriter, <responseObject>.)


Some Links:
XML Serialization in the .NET Framework (Extreme XML)
XML Path Language (XPath)
XPath Tutorial

Share