473,406 Members | 2,273 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

How to convert a string to an xmlNode

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
Jan 4 '06 #1
5 22754
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/U...4-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
"Ken Sturgeon" <ak********@charter.net> wrote in message
news:Oh**************@TK2MSFTNGP12.phx.gbl...
| 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
|
|
Jan 4 '06 #2
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
"Ken Sturgeon" <ak********@charter.net> wrote in message
news:Oh**************@TK2MSFTNGP12.phx.gbl...
| 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
|
|
Jan 4 '06 #3
Ken Sturgeon wrote:
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)
///

--

(O)enone
Jan 4 '06 #4
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
"Jay B. Harlow [MVP - Outlook]" <Ja************@tsbradley.net> wrote in
message news:e6*************@TK2MSFTNGP14.phx.gbl...
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
"Ken Sturgeon" <ak********@charter.net> wrote in message
news:Oh**************@TK2MSFTNGP12.phx.gbl...
| 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
|
|

Jan 4 '06 #5
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

"Oenone" <oe****@nowhere.com> wrote in message
news:WF*******************@newsfe6-win.ntli.net...
Ken Sturgeon wrote:
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)
///

--

(O)enone

Jan 4 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Anita C | last post by:
I have the foll. code to update the value of an attribute: xmlDocument.Load("abc.xml"); XmlAttribute xmlAttrib = xmlDocument.SelectSingleNode(root/web/theme/@desc); xmlAttrib.Value =...
1
by: Svyatoslav | last post by:
Hi, I have a problem with XmlNodes and my stack. It looks something like this: //declarations XmlNode node, new_node; Stack MyStack = new Stack(); //code MyStack.Push(node);
7
by: Jeppe BS | last post by:
hi I got an XmlNode called xNode Is there a way to convert it so it fits into a string ?
8
by: asrs63 | last post by:
Hi, I am a newbee and have a comma seperated flat-file and a DTD. I need to write a C# program which will read the text file and convert it to a XML file as per the the definition in the DTD. I...
5
by: XML newbie: Urgent pls help! | last post by:
function to convert string to 1 dimensional array of long in VB.Net
1
by: Jack | last post by:
Hello, I need to convert a 'System.Xml.XmlNode' to a string. Any help would be great!!! Thanks, Jack
4
by: Java Apache | last post by:
Hi All, I am pretty to new to .NET/C# so go easy! I am developing a Web Service call to a Java Web Service and it is all working fine - it is returning me back a String value which in all...
1
by: shakthi | last post by:
Hi all,....i am developing an pocket PC application which consumes USZip Webservice and display the data. USZip has a GetInfoByZip(String zipcode) method which retuns an XML data...... For...
1
by: Cezus | last post by:
Hello, I cannot convert the following query in the dataset to a string. It says it cannot get more then 2034 chars long... the string just ends at 2034 characters... this is where it goes...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.