473,503 Members | 12,159 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How do I extract XmlNode.InnerText() values on a string?

6 New Member
Hi,

I want to extract the data inside an xmlData to a string.

Let's say I (for example) I have this XML data:

<text display-inline="yes">This<b>is</b>only<ref>a</ref><i>sample</i></text>

using XmlNode.InnerText(), how can I achieve this result?

string[0] = This
string[1] = <b>is</b>
string[2] = only
string[3] = <ref>a</ref>
string[4] = <i>sample</i>

I am still in the process studying XML on C# any help would be appreciated.

Thanks,
Aug 31 '10 #1
3 7797
GaryTexmo
1,501 Recognized Expert Top Contributor
This is from memory but it should be fairly close...

There should be an InnerXML property on an XmlNode object. If you call that on a "text" node, it should give you the "This<b>is</b>..." string as is. You can then parse that string however you like to break it up.

You may be able to parse it as a node structure... I'll take a look and get back to you.
Aug 31 '10 #2
GaryTexmo
1,501 Recognized Expert Top Contributor
As it turns out, this is very easy to do with the node structure. I don't normally do this, but it's probably more worthwhile in this case if I just show you the code than if I try to lead you through it.

Expand|Select|Wrap|Line Numbers
  1.             string xmlRaw = "<text display-inline=\"yes\">This<b>is</b>only<ref>a</ref><i>sample</i></text>";
  2.  
  3.             XmlDocument xmlDoc = new XmlDocument();
  4.             xmlDoc.LoadXml(xmlRaw);
  5.  
  6.             List<string> items = new List<string>();
  7.  
  8.             foreach (XmlNode node in xmlDoc.DocumentElement.ChildNodes)
  9.             {
  10.                 switch (node.NodeType)
  11.                 {
  12.                     case XmlNodeType.Text:
  13.                         items.Add(node.Value);
  14.                         break;
  15.                     case XmlNodeType.Element:
  16.                         items.Add(string.Format("<{0}>{1}</{0}>", node.Name, node.InnerText));
  17.                         break;
  18.                 }
  19.             }
  20.  
  21.             for (int i = 0; i < items.Count; i++)
  22.             {
  23.                 Console.WriteLine(string.Format("items[{0}] = {1}", i.ToString(), items[i]));
  24.             }
This is obviously making liberal use of assumptions, but you can quite easily detect if a node is a text node, then do this with its children to get the same result.

Basically, when it gets read into an XmlDocument object, a hierarchical structure is generated and the appropriate node types are setup for you. It's a simple matter of going through the children of a text node and seeing if they are text, or an element, building the appropriate string, and adding it to an array (list, in this case).

The output of this code is...

items[0] = This
items[1] = <b>is</b>
items[2] = only
items[3] = <ref>a</ref>
items[4] = <i>sample</i>
Aug 31 '10 #3
aredz
6 New Member
Thanks for the reply Gary, I will check on this and let you know if I code this one right.

One thing, It is XmlNode.InnerXml(). Sorry for the confusion.

I will be posting back.

----------------

Thanks for your time on this Gary.

Mods please close this thread. The question has been answered.

Thanks!

aredz~
Sep 1 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
30186
by: rdraider | last post by:
Is there a function that will extract part of a string when the data you want does not occur in a specific position? Field "REF" is varchar(80) and contains an email subject line and the email...
3
23571
by: Dan | last post by:
I've loaded the following xml into an XMLDocument object: <?xml version="1.0" ?> - <Preferences> <Institution>Argh</Institution> <Speaker>Chigier, Ben (1234)</Speaker>...
4
18545
by: Ryan | last post by:
I am trying to write a c# function to extract all numbers out of a string. What is the easiest way to do this, regular expressions? I must account for numbers formatted with or without commas...
1
8722
by: Jack | last post by:
Hello, I need to convert a 'System.Xml.XmlNode' to a string. Any help would be great!!! Thanks, Jack
5
5105
by: deko | last post by:
If I have random and unpredictable user agent strings containing URLs, what is the best way to extract the URL? For example, let's say the string looks like this: registered NYSE 943 <a...
4
7278
by: Ciaran | last post by:
Hi can someone give me hand with this please? What's the best way to extract the extension from the url? example: $string="http://www.domain.co.uk/anypage.html" In this example, I'd be...
0
1239
by: Dev .Net | last post by:
see following to extract links from javascript also http://urenjoy.blogspot.com/2008/10/extract-links-from-string.html
10
3256
by: E11esar | last post by:
Hi there. I have a table (in Oracle) with a column defined as a CLOB and which holds a string of XML data. What I am looking to do is to take this string and search for the <OLD> and <NEW> tags...
2
7412
by: jmartmem | last post by:
Is there a function, or a combination of functions, in MS Access 2007 that will allow me to extract part of a string? I have a database text field "Email" that contains a person's name and email...
0
7264
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
7316
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...
1
6975
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...
1
4992
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...
0
3160
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3148
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1495
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
728
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
371
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.