Groups | Blog | Home
all groups > dotnet xml > may 2004 >

dotnet xml : Weird Issue XML AppendChild


anonymous
5/12/2004 11:32:45 AM
Can you not append a XmlWhiteSpace node to XmlDocument?

See example:

using System;
using System.Xml;

class TestXml
{
public static void Main(string[] args)
{
string xmlDoc = @"<root><child>a</child></root>";

string newXml = @"
<child>b</child>
<child>c</child>
";

XmlDocument d = new XmlDocument();
d.LoadXml(xmlDoc);

XmlElement tmpNode = d.CreateElement("newNodes");
tmpNode.InnerXml = newXml;

//there are 5 nodes, 2 Elements
Console.WriteLine("Number of nodes: " + tmpNode.ChildNodes.Count);
int i = 0;
foreach(XmlNode n in tmpNode)
{
//HMM: should run 5 times right since tmpNode has 5 children but only
runs once!?
//HOWEVER, if you simply add NON-whitespace nodes, it'll work correctly
Console.WriteLine("NODE: " + i++ + ", " + n);
//now append the child node
d.AppendChild(n);
}

}
}

Oleg Tkachenko [MVP]
5/13/2004 10:11:08 AM
[quoted text, click to view]

Should it be foreach(XmlNode n in tmpNode.ChildNodes) ?

--
Oleg Tkachenko [XML MVP]
anonymous
5/14/2004 3:30:36 PM
Oleg-
No difference if you change it.

And..If you change

-->foreach(XmlNode n in tmpNode)

to:

-->foreach(XmlNode n in tmpNode)
{
if(n.NodeType != XmlNodeType.Element) continue;

Still NO GO.

However, this works:

-->foreach(XmlNode n in tmpNode.SelectNodes("//child") )


Truly baffled... or stupid. Probably the latter.

[quoted text, click to view]

Oleg Tkachenko [MVP]
5/16/2004 10:02:43 AM
[quoted text, click to view]

Hmm, both your strings are equal. I meant that's weird to iterate over a
single node. Most likely you want to iterate over tmpNode's childrens?

foreach(XmlNode n in tmpNode.ChildNodes)

--
Oleg Tkachenko [XML MVP]
AddThis Social Bookmark Button