all groups > dotnet xml > september 2004 >
You're in the

dotnet xml

group:

Read node text.


Read node text. MA
9/28/2004 11:19:18 AM
dotnet xml:
Hi all!

I have a function that reads a nodes 'InnerText'. But when the node has
childs 'InnerText' includes that childs text to. Is there a way to select
the value from the node but not from the child?

my xml document structure looks like this:

<Root>
<Depth1>
<Depth2 with attributes>
<Depth3>
<Depth4></Depth4>
<Depth4></Depth4>
<Depth5></Depth5>
<Depth4></Depth4>
</Depth3>
<Depth3>
<Depth4></Depth4>
<Depth4></Depth4>
<Depth4>
<Depth5></Depth5>
</Depth4>
</Depth3>
</Depth2>
<Depth2 with attributes>
<Depth3>
<Depth4></Depth4>
<Depth4></Depth4>
<Depth5></Depth5>
<Depth4></Depth4>
</Depth3>
<Depth3>
<Depth4></Depth4>
<Depth4></Depth4>
<Depth5></Depth5>
<Depth4></Depth4>
</Depth3>
</Depth2>
<Depth1>
</Root>

For each "depth" I have to read its value/text and calculate those values.

Is the question to week, or doés anyone have any ideas?

/Marre

Re: Read node text. Martin Honnen
9/28/2004 1:49:24 PM


[quoted text, click to view]


[quoted text, click to view]

You can easily iterate over the ChildNodes and assemble the direct
content of child text nodes:

public static string GetChildText (XmlNode node) {
string text = "";
foreach (XmlNode child in node.ChildNodes) {
if (child.NodeType == XmlNodeType.Text) {
text += child.Value;
}
}
return text;
}

Then use it e.g.

XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(@"test2004092801.xml");
XmlNodeList nodeList = xmlDocument.GetElementsByTagName("*");
foreach (XmlNode node in nodeList) {
Console.WriteLine("Text is:\r\n{0}\r\n------------\r\n",
GetChildText(node));
}

--

Martin Honnen
Re: Read node text. MA
9/28/2004 2:12:39 PM
[quoted text, click to view]
Ok!
I´ll try that.

Thanks for youre answer!!

/Marre

AddThis Social Bookmark Button