473,657 Members | 2,477 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problems inserting repeating node.

I am trying to do add a repeating XmlNode into and existing XML document and
have had some problems.

The base XML:

<Property>
<Premises>
<Endorsement>
<ShortWording/>
<Wording/>
</Endorsement>
<Address>
<Line1>41 Acacia Avenue</Line1>
<Line2>Town</Line2>
<Line3>City</Line3>
<Postcode>XX9 9 9XX</Postcode>
</Address>
</Premises>
</Property>

I am trying to add two new endorsements, as follows:

<Endorsement>
<ShortWording>s a</ShortWording>
<Wording>wa</Wording>
</Endorsement>
<Endorsement>
<ShortWording>s b</ShortWording>
<Wording>wb</Wording>
</Endorsement>

So initially I tried the following code:

XmlDocument doc = new XmlDocument();
doc.Load("in.xm l"); //Contains the base XML shown above
string insertionPoint = "/*/Premises/Endorsement[1]";
XmlNode childNode = doc.SelectSingl eNode(insertion Point);
XmlElement endorsements = doc.CreateEleme nt("e");
endorsements.In nerXml =
"<Endorsement>< ShortWording>sa </ShortWording><W ording>wa</Wording></Endorsement><En dorsement><Shor tWording>sb</ShortWording><W ording>wb</Wording></Endorsement>";
foreach(XmlNode insertNode in endorsements.Ch ildNodes)
{
doc.SelectSingl eNode(insertion Point).ParentNo de.InsertAfter( insertNode,
childNode);
}

On the first iteration through the foreach loop the insertNode correctly
contains the first ChildNode of the endorsements XMlElement. However on the
second iteration the insertNode contains the <Address> element from the
original document. Why has this suddenly switched to the base XmlDocument?
Where has my second child node in the endorsements variable gone?

So instead I tried to tackle the loop processing as follows:

XmlNodeList nl = endorsements.Ch ildNodes;
for (int count = 0; count <= nl.Count; count++)
{
XmlNode insertNode = nl.Item(count);
doc.SelectSingl eNode(insertion Point).ParentNo de.InsertAfter( insertNode,
childNode);
}

Again, on the first iteration (count = 0) everything is fine. However on
the second iteration (count = 1) the insertNode is a null object as the
XmlNodeList nl now only contains a single item.

Finally I tried changing the statement that initialises the insertNode as
follows:

XmlNode insertNode = nl.Item(0); //Hard coded count to zero

This time it worked. However I have a strong feeling I have just forced the
code to make it work (rather than using the methods "naturally" ) and
therefore this cannot be considered "production " strength.

Can anybody give me any pointers to what I am doing wrong please. Both with
the original "foreach" loop and the subsequent bodged "for" loop.

Thanks for any help.

Paul
Jan 20 '06 #1
1 1315


PaulF wrote:

So instead I tried to tackle the loop processing as follows:

XmlNodeList nl = endorsements.Ch ildNodes;
for (int count = 0; count <= nl.Count; count++)
{
XmlNode insertNode = nl.Item(count);
doc.SelectSingl eNode(insertion Point).ParentNo de.InsertAfter( insertNode,
childNode);
}


ChildNodes is a "live" collection, if you move nodes which are in
ChildNodes then ChildNodes is updated automatically and immediately so
any such attempts with loops and Count fail as your Count changes each
time the loop body is doing that InsertAfter stuff.

You can use e.g.
while (endorsements.H asChildNodes) {
// now simply move endorsements.Fi rstChild for instance
}

And I would use a DocumentFragmen t and simply call InsertAfter or
InsertBefore with the fragment as the argument as that way all children
of the fragment are directly and with one call inserted. Much easier and
much more elegantly in terms of DOM manipulation.
--

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

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

Similar topics

1
4439
by: Martin Danielson | last post by:
I have a problem that I can't work out. I have a script in ASP using VBScript that act quite funny when editing and xml file. Here is my explenation I have the following xml-fil <root><element type="type1">element 1</element><element type="type1">element 2</element><element type="type1">element 3</element><element type="type2">element 4</element><element type="type2">element 5</element></root I wanted to order them by type, by creating...
3
1787
by: Eric Lilja | last post by:
Hello, I'm creating a small utility for an online game. It involves parsing a text file of "tradesskill recipes" and inserting these recipes in a gui tree widget (similar to gui file browsers if you know what I mean). Here's an example of a recipe as it appears in the text file: * Cashew Pie (lvl 39, 5h 3 min, + max power) - Candied Cashew (level 30) -- Cashew -- Refined Honey (level 30) --- Raw Honey
3
6897
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
1
1266
by: John Salerno | last post by:
If I want to create a button that adds text boxes to a form, how would I write the code so that text boxes can be inserted between text boxes that already exist? For example, if this displays properly: Main node 1 - child node (A) - child node (B) - child node
7
2389
by: Andrea | last post by:
Hi there - I'm hoping someone can help me; I've been struggling with this for a few days! :-) I have a webpage that is comprised of many forms containing questions. As the user answers one of the questions, that form is hidden and the next is displayed. Also, as the user answers each question a "count" variable is updated based on their response. I would like the question to show up on the left side and the answer to show up in the...
2
6616
by: nickheppleston | last post by:
I'm trying to iterate through repeating elements to extract data using libxml2 but I'm having zero luck - any help would be appreciated. My XML source is similar to the following - I'm trying to extract the line number and product code from the repeating line elements: <order xmlns="some-ns"> <header> <orderno>123456</orderno> </header>
7
3946
by: petermichaux | last post by:
Hi, I have tried the following based on suggestions of the best way to insert JavaScript into a page. This is instead of using eval(). Unfortunately IE says "unexpected call to property or method access" for the second to last line of my function. If you know what I've done wrong or how to fix it I would appreciate the help.
0
1796
by: BA | last post by:
I posted on this once before and could not get a solution, I am hoping someone can help. I have a very strange code debug behavior that I cannot make heads or tails of: I have c# code being executed in BizTalk assemblies which is repeating debug statements. I tried debug.flush() and debug.close() which did not solve the problem. In my BizTalk process I call a static method:
0
8421
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
8325
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
8844
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
8742
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...
1
8518
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8621
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
7354
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5643
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4330
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.