By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,179 Members | 2,175 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,179 IT Pros & Developers. It's quick & easy.

Inserting Nodes at specific location in XML

P: n/a

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
XmlDocumentFragment. But nothing that seems to cover inserting a
documentFragment at a specific point in the XML document. Any help would be
much appreciated. Thanks.

May 1 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a


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
XmlDocumentFragment. But nothing that seems to cover inserting a
documentFragment 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.Load(@"http://example.com/getResults?from=1&to=10");
XmlElement resultList =
xmlDocument.SelectSingleNode("results/result-list") as XmlElement;
XmlElement next = xmlDocument.SelectSingleNode("results/next") as
XmlElement;
if (next != null) {
XmlTextReader xmlReader = new XmlTextReader(next.InnerText);
xmlReader.MoveToContent();
XmlNode nextResults = xmlDocument.ReadNode(xmlReader);
foreach (XmlNode result in
nextResults.SelectNodes("result-list/result")) {
resultList.AppendChild(result);
}
next.ParentNode.RemoveChild(next);
}

// save somewhere e.g.
xmlDocument.Save(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

P: n/a
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(next.InnerText);
xmlReader.MoveToContent();
XmlNode nextResults = xmlDocument.ReadNode(xmlReader);
foreach (XmlNode result in nextResults.SelectNodes("result-list/result")) {
resultList.AppendChild(result);
}

I am with you to the point where you MoveToContent, but where I get
confused is how you get "nextResults". 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 "nextResults" 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
XmlDocumentFragment. But nothing that seems to cover inserting a
documentFragment 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.Load(@"http://example.com/getResults?from=1&to=10");
XmlElement resultList =
xmlDocument.SelectSingleNode("results/result-list") as XmlElement;
XmlElement next = xmlDocument.SelectSingleNode("results/next") as
XmlElement;
if (next != null) {
XmlTextReader xmlReader = new XmlTextReader(next.InnerText);
xmlReader.MoveToContent();
XmlNode nextResults = xmlDocument.ReadNode(xmlReader);
foreach (XmlNode result in
nextResults.SelectNodes("result-list/result")) {
resultList.AppendChild(result);
}
next.ParentNode.RemoveChild(next);
}

// save somewhere e.g.
xmlDocument.Save(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

P: n/a
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

P: n/a


DWrek wrote:

XmlTextReader xmlReader = new XmlTextReader(next.InnerText);
xmlReader.MoveToContent();
XmlNode nextResults = xmlDocument.ReadNode(xmlReader);
foreach (XmlNode result in nextResults.SelectNodes("result-list/result")) {
resultList.AppendChild(result);
}

I am with you to the point where you MoveToContent, but where I get
confused is how you get "nextResults". 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 "nextResults" 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

P: n/a
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(next.InnerText);
xmlReader.MoveToContent();
XmlNode nextResults = xmlDocument.ReadNode(xmlReader);
foreach (XmlNode result in nextResults.SelectNodes("result-list/result")) {
resultList.AppendChild(result);
}

I am with you to the point where you MoveToContent, but where I get
confused is how you get "nextResults". 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 "nextResults" 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

P: n/a


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/cpconemployingxmlinnetframework.asp>

--

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

This discussion thread is closed

Replies have been disabled for this discussion.