How to convert a string to an xmlNode

K

Ken Sturgeon

Using VB.NET in VS2005, assume that you have a RegEx that matches a specific
html tag; for instance "<title>.*?</title>" that returns a match
"<title>SomeTitleHere</title>" and you want to convert that string into an
xmlNode so you can just grab the InnerHTML... how would you go about
converting it to an xmlNode?

My thanks to all respondents in advance for your help.

Ken
 
J

Jay B. Harlow [MVP - Outlook]

Ken,
Rather then attempt to use RegEx to parse Xml or HTML, I would recommend you
use an Xml or HTML parser to parse Xml or HTML.

For Xml I would recommend the respective classes under the System.Xml
namespace.

For HTML I would recommend the SgmlReader from Got Dot Net:

http://www.gotdotnet.com/Community/...mpleGuid=b90fddce-e60d-43f8-a5c4-c3bd760564bc

You can use the SgmlReader to parse the HTML, while using the SgmlReader as
input to XPathNavigator to select the nodes you want...

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


| Using VB.NET in VS2005, assume that you have a RegEx that matches a
specific
| html tag; for instance "<title>.*?</title>" that returns a match
| "<title>SomeTitleHere</title>" and you want to convert that string into an
| xmlNode so you can just grab the InnerHTML... how would you go about
| converting it to an xmlNode?
|
| My thanks to all respondents in advance for your help.
|
| Ken
|
|
 
J

Jay B. Harlow [MVP - Outlook]

Doh! Clicked send too soon...

I should add, you might be able to use a StringReader to read the string &
pass the StringReader to an XmlReader to parse it...


However! it would probably be easier to have the RegEx with a named group
that returns the "innerText".

Something like:

Dim s1 As String = " ... other html ... <title>SomeTitleHere</title> ...
other html ... "

Static exp As New Regex("<title>(?<innerHtml>.*?)</title>")

Dim match As Match = exp.Match(s1)

If match.Success Then
Debug.WriteLine(match.Groups("innerHtml"), "title's innerHtml")
End If


--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


| Using VB.NET in VS2005, assume that you have a RegEx that matches a
specific
| html tag; for instance "<title>.*?</title>" that returns a match
| "<title>SomeTitleHere</title>" and you want to convert that string into an
| xmlNode so you can just grab the InnerHTML... how would you go about
| converting it to an xmlNode?
|
| My thanks to all respondents in advance for your help.
|
| Ken
|
|
 
O

Oenone

Ken said:
Using VB.NET in VS2005, assume that you have a RegEx that matches a
specific html tag; for instance "<title>.*?</title>" that returns a
match "<title>SomeTitleHere</title>" and you want to convert that
string into an xmlNode so you can just grab the InnerHTML... how
would you go about converting it to an xmlNode?

Just load the string into an XmlDocument:

\\\
Dim someXML As String = "<title>SomeTitleHere</title>"
Dim xmlDoc As New Xml.XmlDocument

xmlDoc.LoadXml(someXML)
MsgBox(xmlDoc.ChildNodes(0).InnerText)
///
 
K

Ken Sturgeon

Wow Jay, that's pretty slick. I didn't realize you could get a named group
out of the RegEx... how cool it that. Man, I have sooo much to learn but
even with my limited skill set it's easy to see that .NET rocks!!

Thanks very much for your help.
Ken
 
K

Ken Sturgeon

I had actually thought along similar lines but wasn't sure how efficient
such an approach would be... and frankly, didn't quite know how to go about
it syntactically. I think that there are places in my app where this
approach as well as that mentioned by Jay will both be useful.

Thank you for your help.

Ken
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top