Connecting Tech Pros Worldwide Help | Site Map

Problems with XmlNode.InsertAfter

The Kiddie
Guest
 
Posts: n/a
#1: Nov 12 '05
Hi,
I am having major headaches with XmlNode.InsertAfter. This is the
format of my XML document

<?xml version="1.0" encoding="utf-8"?>
<Team name="Team">
<Players>
<Player>
<name> GK</name>
<age> 28 </age>
<position> GK</position>
</Player>
<Player>
<name> P1</name>
<age> 22 </age>
<position> GK</position>
</Player>
<Player>
<name> P2</name>
<age> 32 </age>
<position> D</position>
</Player>
<Player>
<name> P3</name>
<age> 27 </age>
<position> D</position>
</Player>
</Players>
<Staff>
<Manager>
<name> Manager </name>
</Manager>
<Assistant-Manager>
<name> Assistant Manager </name>
</Assistant-Manager>
<Physio>
<name> Physio </name>
</Physio>
</Staff>
</Team>

I can locate a node but when I try to do an insert after on that node
I get "The reference node is not a child of this node". This is the
code

XmlNode root2 = doc2.DocumentElement;

//Create a new Element.
XmlElement newPlayer = doc2.CreateElement ("Player");

//doc2.DocumentElement.FirstChild.AppendChild (newPlayer); - this
works but I want to be able to control where exactly the new Player
goes
doc2.InsertAfter (newPlayer, doc2.DocumentElement.FirstChild);

// I have tried finding a Player Node and calling insertafter using
that node as a reference but that fails as well. I'd appreciate any
help.

Also If anyone knows where I can find the Syntax for XPathExpressions
please reply.

All the best,

JD
Martin Honnen
Guest
 
Posts: n/a
#2: Nov 12 '05

re: Problems with XmlNode.InsertAfter




The Kiddie wrote:

[color=blue]
> XmlNode root2 = doc2.DocumentElement;
>
> //Create a new Element.
> XmlElement newPlayer = doc2.CreateElement ("Player");
>
> //doc2.DocumentElement.FirstChild.AppendChild (newPlayer); - this
> works but I want to be able to control where exactly the new Player
> goes
> doc2.InsertAfter (newPlayer, doc2.DocumentElement.FirstChild);[/color]

That should be
doc2.DocumentElement.InsertAfter(newPlayer,
doc2.DocumentElement.FirstChild);

--

Martin Honnen
http://JavaScript.FAQTs.com/
John O Donovan
Guest
 
Posts: n/a
#3: Nov 12 '05

re: Problems with XmlNode.InsertAfter


Thanks for that but it doesn't solve my problem. Ha, I just figured it
out. Here is the code that will work

doc2.DocumentElement.FirstChild.InsertAfter(newPla yer,
doc2.DocumentElement.FirstChild.ChildNodes[6]);

i'm still not exactly sure about the relationship between the node that
comes before the ".insertafter" and the reference node (2nd parameter).

Also can anyone tell me the difference between an XMLNode and an
XMLElement

Cheers,

JD



*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Martin Honnen
Guest
 
Posts: n/a
#4: Nov 12 '05

re: Problems with XmlNode.InsertAfter




John O Donovan wrote:

[color=blue]
> i'm still not exactly sure about the relationship between the node that
> comes before the ".insertafter" and the reference node (2nd parameter).[/color]

It has to be the parent node.




--

Martin Honnen
http://JavaScript.FAQTs.com/
Derek Harmon
Guest
 
Posts: n/a
#5: Nov 12 '05

re: Problems with XmlNode.InsertAfter


"John O Donovan" <jod_oz_work@myrealbox.com> wrote in message news:u8ZbKG2tEHA.1464@TK2MSFTNGP15.phx.gbl...[color=blue]
> Also can anyone tell me the difference between an XMLNode and an
> XMLElement[/color]

XmlElement inherits from the more abstract XmlNode base type. A document is
composed of a bunch of nodes, nodes can be contained in other nodes, and this
simple structure is what creates the hierarchical (tree-like) form of an XML doc.

Here's the skinny on Node from the source,

http://www.w3.org/TR/2004/REC-DOM-Le...#ID-1950641247

you'll notice that as it's defined by the W3C, Node is an interface. This means
a node is never created directly. Instead, a specialized implementation of a Node
is what's created, and there's are several of these concrete nodes (comments,
processing instructions, CDATA sections, entity references, text, attributes,
etc.)

An element is a special kind of node, and it normally has a string representation
involving angle brackets, like this <name></name> or <name />. Elements can
contain attribute nodes, text nodes, other subordinate element nodes, comment
nodes, CDATA section nodes, entity references, processing instructions ...

The official definition is here,

http://www.w3.org/TR/2004/REC-DOM-Le...l#ID-745549614

In summary, everything is a node. Only tags are elements.


Derek Harmon


Closed Thread


Similar .NET Framework bytes