Solved

Referencing XML nodes?

Posted on 2004-03-26
2
244 Views
Last Modified: 2010-04-15
Hi,

This seems like it should be a straight forward question...

I just basically want to do DOM programming in C#, but I can't seem to find the methods I am familiar with from Javascript. I don't have much experience with this.

I want to access specific nodes of an xml document with array type notation as in child[1].child[1]. Actually, I think I need to do this with variables as in child[int].child[int].

my xml document looks something like this

<root>
   <items>
      <item/>
      <item/>
      <item/>
   </items>
  <items>
      <item/>
      <item/>
      <item/>
   </items>
  <items>
      <item/>
      <item/>
      <item/>
   </items>
</root>

 now imagine that I had an array string[] where I wanted to set the contents of string[] to the item values of one of those items elements such that...

string[] string = {"test1", "test2", "test3"};

ends up as

<items>
   <item>test1<item/>
   <item>test2<item/>
   <item>test3<item/>
<items/>



I am thinking something like

for(i=0; i<number;i++){
   XmlclassInstance.root.items[3].item[i] = string[i];
}

Does this make sense to anyone? Can anyone write a little piece of code instantiating the appropriate class instance using the right methods, with the right namespace declaration if non-standard? If I have this much I think I can figure out how to deal with looping the correct number of times and the other oddities of my real situation.

So far I have just been bumbling around with XPath, XPathNavigator, XmlDocument, but these don't seem to give me access to the members I need.

Thanks a lot in advance!

0
Comment
Question by:coltrane2003
2 Comments
 
LVL 2

Accepted Solution

by:
sedmans earned 125 total points
ID: 10686369
Try using the ChildNodes property instead of the Items.  Check out this MSDN article.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemXmlXmlNodeClassChildNodesTopic.asp
0
 
LVL 2

Author Comment

by:coltrane2003
ID: 10690488
Thanks sedmans! This is just what I needed.

I guess it is too elementary to be covered by my book. This is important because I don't think XPath or XPathNavigator allow you to write an expression with variables.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video discusses moving either the default database or any database to a new volume.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now