473,513 Members | 3,895 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>XX99 9XX</Postcode>
</Address>
</Premises>
</Property>

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

<Endorsement>
<ShortWording>sa</ShortWording>
<Wording>wa</Wording>
</Endorsement>
<Endorsement>
<ShortWording>sb</ShortWording>
<Wording>wb</Wording>
</Endorsement>

So initially I tried the following code:

XmlDocument doc = new XmlDocument();
doc.Load("in.xml"); //Contains the base XML shown above
string insertionPoint = "/*/Premises/Endorsement[1]";
XmlNode childNode = doc.SelectSingleNode(insertionPoint);
XmlElement endorsements = doc.CreateElement("e");
endorsements.InnerXml =
"<Endorsement><ShortWording>sa</ShortWording><Wording>wa</Wording></Endorsement><Endorsement><ShortWording>sb</ShortWording><Wording>wb</Wording></Endorsement>";
foreach(XmlNode insertNode in endorsements.ChildNodes)
{
doc.SelectSingleNode(insertionPoint).ParentNode.In sertAfter(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.ChildNodes;
for (int count = 0; count <= nl.Count; count++)
{
XmlNode insertNode = nl.Item(count);
doc.SelectSingleNode(insertionPoint).ParentNode.In sertAfter(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 1311


PaulF wrote:

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

XmlNodeList nl = endorsements.ChildNodes;
for (int count = 0; count <= nl.Count; count++)
{
XmlNode insertNode = nl.Item(count);
doc.SelectSingleNode(insertionPoint).ParentNode.In sertAfter(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.HasChildNodes) {
// now simply move endorsements.FirstChild for instance
}

And I would use a DocumentFragment 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
4428
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...
3
1774
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...
3
6834
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
1264
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
2368
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...
2
6603
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
3928
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
1784
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...
0
7178
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...
0
7563
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...
0
7543
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...
0
5703
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...
1
5102
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4757
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...
0
3239
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
813
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
470
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...

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.