Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / XML / September 2004

Tip: Looking for answers? Try searching our database.

Read node text.

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
MA - 28 Sep 2004 10:19 GMT
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
Martin Honnen - 28 Sep 2004 12:49 GMT
> 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?

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));
    }

Signature

    Martin Honnen
    http://JavaScript.FAQTs.com/

MA - 28 Sep 2004 13:12 GMT
>> 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
[quoted text clipped - 22 lines]
> GetChildText(node));
>     }

Ok!
I?ll try that.

Thanks for youre answer!!

/Marre

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.