473,883 Members | 1,796 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Inserting Nodes at specific location in XML


Here is my problem. I have an XML document that is returned to me by a
third party service. The XML document contains results for a search but only
lists a maximum of 10 results. If there are any further results then the XML
document contains a URL to call and get the next set of 10 results. So if
there were 25 results total I would get the first 10 in the first XML
document, then after making a second request I would get results 11 to 20 and
then after a third request I would get results 21 to 15.

Here is an axample of the structure of the returned results.

<root>
<nodeA>
<nodeC>
<nodeD>
<result1>
Value1
</result1>
<result2>
Value2
</result2>
<result3>
Value3
</result3>
...
<result10>
Value10
</result10>
<-- I Want to insert results from 2nd and 3rd requests here
</nodeD>
</nodeC>
</nodeA>
<nodeB>
</nodeB>
</root>
What I need to do is assemble all the results as one XML file. In scanning
searches via Google I am finding a lot of references to using the
XmlDocumentFrag ment. But nothing that seems to cover inserting a
documentFragmen t at a specific point in the XML document. Any help would be
much appreciated. Thanks.

May 1 '06 #1
6 2729


DWrek wrote:
Here is my problem. I have an XML document that is returned to me by a
third party service. The XML document contains results for a search but only
lists a maximum of 10 results. If there are any further results then the XML
document contains a URL to call and get the next set of 10 results. So if
there were 25 results total I would get the first 10 in the first XML
document, then after making a second request I would get results 11 to 20 and
then after a third request I would get results 21 to 15. What I need to do is assemble all the results as one XML file. In scanning
searches via Google I am finding a lot of references to using the
XmlDocumentFrag ment. But nothing that seems to cover inserting a
documentFragmen t at a specific point in the XML document.


If you use the DOM and you have an XmlDocument instance then you can use
all kind of DOM methods like AppendChild, ReplaceChild, InsertBefore to
insert nodes at a specific position.

For instance if you have one document as

<?xml version="1.0" encoding="UTF-8"?>
<results>
<result-list>
<result>1</result>
<result>2</result>
<result>3</result>
<result>4</result>
<result>5</result>
<result>6</result>
<result>7</result>
<result>8</result>
<result>9</result>
<result>10</result>
</result-list>
<next>http://example.com/getResults?from =11&amp;to=20</next>
</results>

and http://example.com/getResults?from=11&to=20 delivers the next 10
results then you could use e.g.
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Loa d(@"http://example.com/getResults?from =1&to=10");
XmlElement resultList =
xmlDocument.Sel ectSingleNode(" results/result-list") as XmlElement;
XmlElement next = xmlDocument.Sel ectSingleNode(" results/next") as
XmlElement;
if (next != null) {
XmlTextReader xmlReader = new XmlTextReader(n ext.InnerText);
xmlReader.MoveT oContent();
XmlNode nextResults = xmlDocument.Rea dNode(xmlReader );
foreach (XmlNode result in
nextResults.Sel ectNodes("resul t-list/result")) {
resultList.Appe ndChild(result) ;
}
next.ParentNode .RemoveChild(ne xt);
}

// save somewhere e.g.
xmlDocument.Sav e(Console.Out);

then the saved result is

<results>
<result-list>
<result>1</result>
<result>2</result>
<result>3</result>
<result>4</result>
<result>5</result>
<result>6</result>
<result>7</result>
<result>8</result>
<result>9</result>
<result>10</result>
<result>11</result>
<result>12</result>
<result>13</result>
<result>14</result>
<result>15</result>
<result>16</result>
<result>17</result>
<result>18</result>
<result>19</result>
<result>20</result>
</result-list>
</results>

Obviously you need to tune that to look for further results until no
more are found but the example should suffice to show you how to read in
from a URL to get a node and how to insert the nodes at the right position.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
May 1 '06 #2
Hi Martin,

Thanks for helping out. Your sample code made a lot of sense to me but I
remain confused on one section:

XmlTextReader xmlReader = new XmlTextReader(n ext.InnerText);
xmlReader.MoveT oContent();
XmlNode nextResults = xmlDocument.Rea dNode(xmlReader );
foreach (XmlNode result in nextResults.Sel ectNodes("resul t-list/result")) {
resultList.Appe ndChild(result) ;
}

I am with you to the point where you MoveToContent, but where I get
confused is how you get "nextResult s". Is the XMLDocument going out and
calling the URL that is contained in the "InnerText" of the "next" node? How
do you end up with the "nextResult s" collection?

- Derek
"Martin Honnen" wrote:


DWrek wrote:
Here is my problem. I have an XML document that is returned to me by a
third party service. The XML document contains results for a search but only
lists a maximum of 10 results. If there are any further results then the XML
document contains a URL to call and get the next set of 10 results. So if
there were 25 results total I would get the first 10 in the first XML
document, then after making a second request I would get results 11 to 20 and
then after a third request I would get results 21 to 15.

What I need to do is assemble all the results as one XML file. In scanning
searches via Google I am finding a lot of references to using the
XmlDocumentFrag ment. But nothing that seems to cover inserting a
documentFragmen t at a specific point in the XML document.


If you use the DOM and you have an XmlDocument instance then you can use
all kind of DOM methods like AppendChild, ReplaceChild, InsertBefore to
insert nodes at a specific position.

For instance if you have one document as

<?xml version="1.0" encoding="UTF-8"?>
<results>
<result-list>
<result>1</result>
<result>2</result>
<result>3</result>
<result>4</result>
<result>5</result>
<result>6</result>
<result>7</result>
<result>8</result>
<result>9</result>
<result>10</result>
</result-list>
<next>http://example.com/getResults?from =11&to=20</next>
</results>

and http://example.com/getResults?from=11&to=20 delivers the next 10
results then you could use e.g.
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Loa d(@"http://example.com/getResults?from =1&to=10");
XmlElement resultList =
xmlDocument.Sel ectSingleNode(" results/result-list") as XmlElement;
XmlElement next = xmlDocument.Sel ectSingleNode(" results/next") as
XmlElement;
if (next != null) {
XmlTextReader xmlReader = new XmlTextReader(n ext.InnerText);
xmlReader.MoveT oContent();
XmlNode nextResults = xmlDocument.Rea dNode(xmlReader );
foreach (XmlNode result in
nextResults.Sel ectNodes("resul t-list/result")) {
resultList.Appe ndChild(result) ;
}
next.ParentNode .RemoveChild(ne xt);
}

// save somewhere e.g.
xmlDocument.Sav e(Console.Out);

then the saved result is

<results>
<result-list>
<result>1</result>
<result>2</result>
<result>3</result>
<result>4</result>
<result>5</result>
<result>6</result>
<result>7</result>
<result>8</result>
<result>9</result>
<result>10</result>
<result>11</result>
<result>12</result>
<result>13</result>
<result>14</result>
<result>15</result>
<result>16</result>
<result>17</result>
<result>18</result>
<result>19</result>
<result>20</result>
</result-list>
</results>

Obviously you need to tune that to look for further results until no
more are found but the example should suffice to show you how to read in
from a URL to get a node and how to insert the nodes at the right position.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/

May 1 '06 #3
Hi Derek,

Based on the code provided by Martin, yes, it is using the xmlDocument to
create and XmlNode with the information from XmlTextReader. Then the node
can be added to the xmlDocument itself. It seems to be the most convenient
way.

Kevin Yu
Microsoft Online Community Support

=============== =============== =============== =============== =============== =
=============== ===========
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
=============== =============== =============== =============== =============== =
=============== ===========

(This posting is provided "AS IS", with no warranties, and confers no
rights.)

May 2 '06 #4


DWrek wrote:

XmlTextReader xmlReader = new XmlTextReader(n ext.InnerText);
xmlReader.MoveT oContent();
XmlNode nextResults = xmlDocument.Rea dNode(xmlReader );
foreach (XmlNode result in nextResults.Sel ectNodes("resul t-list/result")) {
resultList.Appe ndChild(result) ;
}

I am with you to the point where you MoveToContent, but where I get
confused is how you get "nextResult s". Is the XMLDocument going out and
calling the URL that is contained in the "InnerText" of the "next" node?
Yes, in my example the contents of the next element is the URL to access
to load the next results, that is why I create an XmlTextReader
accessing that URL.

How
do you end up with the "nextResult s" collection?


The XmlDocument has a method ReadNode which takes an XmlTextReader as
the argument and reads from that reader to create a node (and its
subtree) owned by the document so that you can insert the node somewhere
into the document. Then my example uses XPath and SelectNodes on that
node that ReadNode created so that the nodes you need, in the example
the result elements, are selected to be then moved at the proper place
in the XmlDocument instance.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
May 2 '06 #5
Hi Martin,

So if I understand correctly, the variable "resultList " is owned by the
document therefore any changes made to "resultList " are persisted to the XML
document as a whole.

Thanks for all your help. Your example was very good and helped me
understand what you were talking about. One problem I am finding in using XML
from within .NET is that there are so many different ways to accomplish the
same thing. Most of the time I am not sure which object is best to use for a
given problem. Can you suggest any book or resource that explains in detail
the various objects within the .NET XML library? Again, thanks for your help.

- dwok

"Martin Honnen" wrote:


DWrek wrote:

XmlTextReader xmlReader = new XmlTextReader(n ext.InnerText);
xmlReader.MoveT oContent();
XmlNode nextResults = xmlDocument.Rea dNode(xmlReader );
foreach (XmlNode result in nextResults.Sel ectNodes("resul t-list/result")) {
resultList.Appe ndChild(result) ;
}

I am with you to the point where you MoveToContent, but where I get
confused is how you get "nextResult s". Is the XMLDocument going out and
calling the URL that is contained in the "InnerText" of the "next" node?


Yes, in my example the contents of the next element is the URL to access
to load the next results, that is why I create an XmlTextReader
accessing that URL.

How
do you end up with the "nextResult s" collection?


The XmlDocument has a method ReadNode which takes an XmlTextReader as
the argument and reads from that reader to create a node (and its
subtree) owned by the document so that you can insert the node somewhere
into the document. Then my example uses XPath and SelectNodes on that
node that ReadNode created so that the nodes you need, in the example
the result elements, are selected to be then moved at the proper place
in the XmlDocument instance.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/

May 2 '06 #6


DWrek wrote:
Can you suggest any book or resource that explains in detail
the various objects within the .NET XML library?


MSDN has a section on that
<http://msdn.microsoft. com/library/default.asp?url =/library/en-us/cpguide/html/cpconemployingx mlinnetframewor k.asp>

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
May 2 '06 #7

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

Similar topics

0
1491
by: indo3 | last post by:
Hello, I program an xml editor with xerces for the university of Braunschweig, the user dont see any xml tags, it is represented in a JTree. Now the user shall be able to insert nodes, which conforms to the schema file (the user need not know what that is, its transparent to him). I found two approaches for doing that: 1. When xerces validates the source or the DOM tree (this is a new
2
2080
by: Lutz IŖler | last post by:
Hi all! I dymically replace the child nodes of a DIV element by image nodes. In IE and Firefox this works properly. In Safari it works properly, too - but only if the site is called locally, eg. with file://. Uploading to a server and viewing the site in Safari with http:// results in an error. The problematic code is the following: var node = document.createElement("img");
2
10706
by: Greg | last post by:
Hi. I have a rather large xml document (object) that can have one or more nodes with a certain attribute throughout (at ANY depth, not at the same level necessarily). I need to find this attribute and remove the containing node (and child nodes) if it has a certain value. I'm able to find the attributes using an XmlTextReader. Once found, can someone help me get the XPath at that point? I would then use this to remove the node from...
3
6938
by: Joachim Klassen | last post by:
Hi all, first apologies if this question looks the same as another one I recently posted - its a different thing but for the same szenario:-). We are having performance problems when inserting/deleting rows from a large table. My scenario: Table (lets call it FACT1) with 1000 million rows distributed on 12
6
2268
by: jw | last post by:
hi all,i have such 2 classes for a linked list, class Node{ private: int value; Node *next; public: Node() { next=NULL; }
9
5007
by: Mohd Al Junaibi | last post by:
Hello all, my first post here...hope it goes well. I'm currently working on stored procedure where I translated some reporting language into T-SQL The logic: I have a group of tables containing important values for calculation. I run various sum calculations on various fields in order to retrieve cost calculations ...etc.
20
16656
by: alice | last post by:
I'm doing some text swapping with javascript, got it working fine, but I would like the line to have line breaks and being a beginner, I don't even know if this is possible. So I have a line like this swaptext('Shaun, Greg, Violet, Joel, 1999')"; and I would like 1999 to be on a new line. Is there a simple way to do this? I'd swear I've seen a way, but now I can't seem to find it.
2
1971
by: SMH | last post by:
I am in the process of converting all HTML documents, including many dynamic/interactive documents, to XHTML documents (because I want to incorporate SVG and MathML, among other things). I am having a problem converting document.write() statements which are not allowed in XHTML documents to statements which create the DOM nodes/elements dynamically within script elements.
5
13114
by: atlanteavila | last post by:
Hello all, I have a problem with inserting text with quotes, or html code in to a MYSql Database. I've been trying to create my own content management system, and unfortunately I've come accross a problem, the script below works perfectly if I'm not inserting any quotes (" ") or html tags. I'm wondering if I'm doing something wrong here? The field type where all of the content will go was set up as a blob type. I have the following PHP...
0
9942
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
9792
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11142
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10743
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10416
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5797
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5991
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4220
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3233
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.