473,320 Members | 2,193 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,320 software developers and data experts.

Getting the XML as text between two nodes

Hello,

I am interested in getting the XML contents as text between two XML elements
that I know follow each other. They could be in completely different levels,
but in XML file the first is above the second. Of course, a code could be
written that would first read InnerXml property of the start tag, extract
the opening parens <starttag>, then read all text nodes within, then do the
same with child nodes, then with start tag next siblings, then back up to
parent node and advance to next parent sibling, etc, etc, until the end node
is met. But that seems like exhaustive thing to do, and I am wondering if
there's some normal shorter way to do this. Now I know from XML-speak, that
there is such thing as XPointers, that allow some syntax to select a range
of characters. Perhaps it's kinda underdeveloped standard, as I don't see
much talk about using XPointers in .NET framework. Anyway, I don't really
care for standard, all I'd like to be able is to provide to method two
parameters - starting element node and ending element node, and it would
return string of non-self-contained XML what's between (including the start
element node itself)

Thanks,

-- Pavils

Some examples for reference

Input:

<blah>
<startTag>
<foo>
<too>
<endTag/>
</foo>
</foo>
</startTag>
</blah>

result:
<startTag>
<foo>
<too>

Input:

<blah>
<too>
<foo>
<too>
<startTag/>
</foo>
</foo>
</foo>
<endTag/>
</blah>

restult:
<startTag/>
</foo>
</foo>
</foo>
Nov 12 '05 #1
4 2344
Here is an example:
XmlDocument doc = null;
XmlElement nodeBeforeTxt = null;
try
{
doc = new XmlDocument();
doc.LoadXml("<Root><Element1/><Element2/></Root>");
nodeBeforeTxt = (XmlElement)doc.SelectSingleNode("Root/Element1");
doc.SelectSingleNode("Root").InsertAfter(doc.Creat eTextNode("Text
between nodes"), nodeBeforeTxt);
MessageBox.Show(doc.OuterXml);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}

"Pavils Jurjans" <pa****@mailbox.riga.lv> skrev i meddelandet
news:Ot**************@tk2msftngp13.phx.gbl...
Hello,

I am interested in getting the XML contents as text between two XML elements that I know follow each other. They could be in completely different levels, but in XML file the first is above the second. Of course, a code could be
written that would first read InnerXml property of the start tag, extract
the opening parens <starttag>, then read all text nodes within, then do the same with child nodes, then with start tag next siblings, then back up to
parent node and advance to next parent sibling, etc, etc, until the end node is met. But that seems like exhaustive thing to do, and I am wondering if
there's some normal shorter way to do this. Now I know from XML-speak, that there is such thing as XPointers, that allow some syntax to select a range
of characters. Perhaps it's kinda underdeveloped standard, as I don't see
much talk about using XPointers in .NET framework. Anyway, I don't really
care for standard, all I'd like to be able is to provide to method two
parameters - starting element node and ending element node, and it would
return string of non-self-contained XML what's between (including the start element node itself)

Thanks,

-- Pavils

Some examples for reference

Input:

<blah>
<startTag>
<foo>
<too>
<endTag/>
</foo>
</foo>
</startTag>
</blah>

result:
<startTag>
<foo>
<too>

Input:

<blah>
<too>
<foo>
<too>
<startTag/>
</foo>
</foo>
</foo>
<endTag/>
</blah>

restult:
<startTag/>
</foo>
</foo>
</foo>

Nov 12 '05 #2
Here is an example:
XmlDocument doc = null;
XmlElement nodeBeforeTxt = null;
try
{
doc = new XmlDocument();
doc.LoadXml("<Root><Element1/><Element2/></Root>");
nodeBeforeTxt = (XmlElement)doc.SelectSingleNode("Root/Element1");
doc.SelectSingleNode("Root").InsertAfter(doc.Creat eTextNode("Text
between nodes"), nodeBeforeTxt);
MessageBox.Show(doc.OuterXml);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}

"Pavils Jurjans" <pa****@mailbox.riga.lv> skrev i meddelandet
news:Ot**************@tk2msftngp13.phx.gbl...
Hello,

I am interested in getting the XML contents as text between two XML elements that I know follow each other. They could be in completely different levels, but in XML file the first is above the second. Of course, a code could be
written that would first read InnerXml property of the start tag, extract
the opening parens <starttag>, then read all text nodes within, then do the same with child nodes, then with start tag next siblings, then back up to
parent node and advance to next parent sibling, etc, etc, until the end node is met. But that seems like exhaustive thing to do, and I am wondering if
there's some normal shorter way to do this. Now I know from XML-speak, that there is such thing as XPointers, that allow some syntax to select a range
of characters. Perhaps it's kinda underdeveloped standard, as I don't see
much talk about using XPointers in .NET framework. Anyway, I don't really
care for standard, all I'd like to be able is to provide to method two
parameters - starting element node and ending element node, and it would
return string of non-self-contained XML what's between (including the start element node itself)

Thanks,

-- Pavils

Some examples for reference

Input:

<blah>
<startTag>
<foo>
<too>
<endTag/>
</foo>
</foo>
</startTag>
</blah>

result:
<startTag>
<foo>
<too>

Input:

<blah>
<too>
<foo>
<too>
<startTag/>
</foo>
</foo>
</foo>
<endTag/>
</blah>

restult:
<startTag/>
</foo>
</foo>
</foo>

Nov 12 '05 #3
Hello Magnus,

I am not sure if you understood me correctly...

I am talking not about content insertion, but content retrieval, as string.
I am looking for a code block that could be given two element nodes, and it
would return _string_ of all XML that's between those nodes. Imagine that
you open XML file in text editor, identify the starting node, then the
ending node, and select the text, including the starting node "<blah>" tag,
and ending the last character before the closing node's "<blah>" tag. So, I
want to extract this text and return it as string.

-- Pavils
Nov 12 '05 #4
Hello Magnus,

I am not sure if you understood me correctly...

I am talking not about content insertion, but content retrieval, as string.
I am looking for a code block that could be given two element nodes, and it
would return _string_ of all XML that's between those nodes. Imagine that
you open XML file in text editor, identify the starting node, then the
ending node, and select the text, including the starting node "<blah>" tag,
and ending the last character before the closing node's "<blah>" tag. So, I
want to extract this text and return it as string.

-- Pavils
Nov 12 '05 #5

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

Similar topics

7
by: F. Da Costa | last post by:
Hi, I' looking to retrieve ProdName1 form the <tr> below. <tr id="1-1-1" class="even"> <td> <div class="tier4"> <a href="#" class="leaf"></a> ProdName1 </div>
4
by: leodippolito | last post by:
Hello sirs, I am trying to send a POST request to a webservice on the click of a button. This will return me an XML document with a list of combo box items. The problem: in FIREFOX, when the...
0
by: Pavils Jurjans | last post by:
Hello, I am interested in getting the XML contents as text between two XML elements that I know follow each other. They could be in completely different levels, but in XML file the first is...
2
by: metaperl | last post by:
Hello, I'm trying to get the MSDN documentation example of a treeview to work: http://msdn2.microsoft.com/en-us/library/system.windows.forms.treeview.aspx I made the function static and added a...
4
by: R.Manikandan | last post by:
Hi In my code, one string variable is subjected to contain more amount of characters. If it cross certain limit, the string content in the varabile is automatically getting truncated and i am...
2
by: wolfgang.schwarz | last post by:
hi, is there a way of getting the position and size of a text node in Firefox without altering the DOM? For example, suppose I have a document fragment like this: <div> textnode1 <img>...
7
by: Arancaytar | last post by:
(Note: I am a Javascript newbie. I can handle PHP and Java, but this is unfamiliar territory.) For a wordcount feature, I need to collect the complete text content of a 'div' element inside a...
3
by: Andrej Pavlovic | last post by:
Hi, Please look at the following HTML snippet: <body> some text <p>more text</p> even more text </body>
8
by: Jon Slaughter | last post by:
if I call javascript like <a href="javascript:;" onmousedown="toggle('a');">O</a> is there a way to pass the element a to the function without specifying any tags or names? I want the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.