473,405 Members | 2,185 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Xml Node Access

Hello.

I have a problem to accessing the right clindNodes from the xml.
I have many "step" elements with id.
I want to get the attributes from the 5 "matrix" elements, depending on
parent "step" id ( example - if some var(MyVariable) = 2, the code
should get the 5 matrix attributes that have a parent <step id="2" >).
My codea always returning the 5 matrix attributes only from step
id="1", and does not mather that i change the MyVariable to 2, 3 ..
Please help

This is my code:

mainForm.xmldoc = new XmlDocument();
mainForm.xmldoc.Load("file.xml");
XmlNode onode = mainForm.xmldoc.DocumentElement;

XmlNodeList xmlnode =
mainForm.xmldoc.GetElementsByTagName("step");
for (int i = 0; i < xmlnode.Count; i++)
{
XmlAttributeCollection xmlattrc =
xmlnode[i].Attributes;
if (xmlnode[i].Attributes[0].Value.ToString() ==
MyVariable)
{
matnode =
mainForm.xmldoc.GetElementsByTagName("matrix");

for (int a = 0; a < matnode.Count; a++ ) {
matatt = matnode[a].Attributes;

}
}
}
there is the part of the xml:

<?xml version="1.0"?>
<DocRoot>
<steps>
<step id="1">
<matrixs>
<matrix mrp="20" i="100,5" lf="10,2" r="0,3" />
<matrix mrp="40" i="300" lf="50" r="20" />
<matrix mrp="60" i="500" lf="30" r="15" />
<matrix mrp="80" i="900" lf="20" r="8" />
<matrix mrp="100" i="1300" lf="90" r="52" />
</matrixs>
<vals it="0" mrp="20" soe="0" shi="0" irr="0" pp="0" />
</step>
<step id="2">
<matrixs>
<matrix mrp="20" i="100,5" lf="10,2" r="0,3" />
<matrix mrp="40" i="300" lf="50" r="20" />
<matrix mrp="60" i="500" lf="30" r="15" />
<matrix mrp="80" i="900" lf="20" r="8" />
<matrix mrp="100" i="1300" lf="90" r="52" />
</matrixs>
<vals it="0" mrp="20" soe="0" shi="0" irr="0" pp="0" />
</step>
..
..
<DocRoot>
Thanks in advance

Jun 20 '06 #1
4 4031

sitemap wrote:
Hello.

I have a problem to accessing the right clindNodes from the xml.
I have many "step" elements with id.
I want to get the attributes from the 5 "matrix" elements, depending on
parent "step" id ( example - if some var(MyVariable) = 2, the code
should get the 5 matrix attributes that have a parent <step id="2" >).
My codea always returning the 5 matrix attributes only from step
id="1", and does not mather that i change the MyVariable to 2, 3 ..
Please help

This is my code:

mainForm.xmldoc = new XmlDocument();
mainForm.xmldoc.Load("file.xml");
XmlNode onode = mainForm.xmldoc.DocumentElement;

XmlNodeList xmlnode =
mainForm.xmldoc.GetElementsByTagName("step");
for (int i = 0; i < xmlnode.Count; i++)
{
XmlAttributeCollection xmlattrc =
xmlnode[i].Attributes;
if (xmlnode[i].Attributes[0].Value.ToString() ==
MyVariable)
{
matnode =
mainForm.xmldoc.GetElementsByTagName("matrix");


This line is wrong. It means you are always getting the first matrix
nodes from the *document*, not from underneath the particular step node
you've just found. I think you want to cast xmlnode[i] to XmlElement,
then invoke GetElementsByTagName on that, rather than on xmldoc.

Or you could just use XPath on the original document, couldn't you?
--
Larry Lard
Knows enough XML to be dangerous
Replies to group please

Jun 20 '06 #2
Thank you Larry.

I never beeen using the Xpath in the C# - only in my Perl codes and i
think that it will be dificult for me.
May be i will try the other way. but i cannot find the info, how to
cast xmlnode to XmlElement. Can you tell me - from where i can find the
info in the net or msdn - url or name of the method or other?

Larry Lard wrote:
sitemap wrote:
Hello.

I have a problem to accessing the right clindNodes from the xml.
I have many "step" elements with id.
I want to get the attributes from the 5 "matrix" elements, depending on
parent "step" id ( example - if some var(MyVariable) = 2, the code
should get the 5 matrix attributes that have a parent <step id="2" >).
My codea always returning the 5 matrix attributes only from step
id="1", and does not mather that i change the MyVariable to 2, 3 ..
Please help

This is my code:

mainForm.xmldoc = new XmlDocument();
mainForm.xmldoc.Load("file.xml");
XmlNode onode = mainForm.xmldoc.DocumentElement;

XmlNodeList xmlnode =
mainForm.xmldoc.GetElementsByTagName("step");
for (int i = 0; i < xmlnode.Count; i++)
{
XmlAttributeCollection xmlattrc =
xmlnode[i].Attributes;
if (xmlnode[i].Attributes[0].Value.ToString() ==
MyVariable)
{
matnode =
mainForm.xmldoc.GetElementsByTagName("matrix");


This line is wrong. It means you are always getting the first matrix
nodes from the *document*, not from underneath the particular step node
you've just found. I think you want to cast xmlnode[i] to XmlElement,
then invoke GetElementsByTagName on that, rather than on xmldoc.

Or you could just use XPath on the original document, couldn't you?
--
Larry Lard
Knows enough XML to be dangerous
Replies to group please


Jun 20 '06 #3

sitemap wrote:
Thank you Larry.

I never beeen using the Xpath in the C# - only in my Perl codes and i
think that it will be dificult for me.
May be i will try the other way. but i cannot find the info, how to
cast xmlnode to XmlElement.


Just change

matnode =
mainForm.xmldoc.GetElementsByTagName("matrix");

to

matnode =
((XmlElement)xmlnode[i]).GetElementsByTagName("matrix");

And you should really take fifteen minutes to look up the syntax for
using XPath - what you are doing here is what XPath is for. Even I've
managed to get some XPath stuff working, and I'm just a dumb VB
programmer.

--
Larry Lard
Replies to group please

Jun 21 '06 #4
Thank you!!

but i found ather way to do this ( it is little longer .. but works :)
)

private XmlAttributeCollection _a;
public XmlAttributeCollection popvals(int ind)
{
mainForm.xmldoc = new XmlDocument();
mainForm.xmldoc.Load(mainForm.pdir);
XmlNode onode = mainForm.xmldoc.DocumentElement;
XmlAttributeCollection xmlattrc;
XmlNodeList xmlnode =
mainForm.xmldoc.GetElementsByTagName("step");
for (int i = 0; i < xmlnode.Count; i++)
{
xmlattrc = xmlnode[i].Attributes;
if (xmlnode[i].Attributes[0].Value.ToString() ==
ind.ToString())
{
_a = xmlnode[i].ChildNodes[1].Attributes;
}
}
mainForm.xmldoc = null;
return _a;
}

Thank you again.
Niky
Larry Lard wrote:
sitemap wrote:
Thank you Larry.

I never beeen using the Xpath in the C# - only in my Perl codes and i
think that it will be dificult for me.
May be i will try the other way. but i cannot find the info, how to
cast xmlnode to XmlElement.


Just change

matnode =
mainForm.xmldoc.GetElementsByTagName("matrix");

to

matnode =
((XmlElement)xmlnode[i]).GetElementsByTagName("matrix");

And you should really take fifteen minutes to look up the syntax for
using XPath - what you are doing here is what XPath is for. Even I've
managed to get some XPath stuff working, and I'm just a dumb VB
programmer.

--
Larry Lard
Replies to group please


Jun 22 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: gotdough | last post by:
Cluster services gives the high availability needed - that is great. But I have never seen any discussion about what happens when a node fails - what do you do to get everything back to the...
4
by: Rolf Kemper | last post by:
Hi All, ned help on the example below. It works fine for msxml3/4 but has problems with saxon. Saxon complains "can not find matching function ..... " My target is to write style sheets...
6
by: SHC | last post by:
Hi all, I created an application from the Console Application (.NET) of VC++ .NET 2003, and I did "Build" the application of the attached .cpp file, volcanoes.xml and geology.dtd on my VC++ .NET...
6
by: Chuck | last post by:
What's the difference between using "catalog node" and catalog admin node"? -- Chuck Remove "_nospam" to reply by email
3
by: Saradhi | last post by:
Hi All, Here I am facing a performance problem with the TreeView Node renaming. I am displaying a hierarchy Data in a treeview in my Windows C# Application. My tree view represents an...
5
by: poldoj | last post by:
Hi all, I have a treenode control and I would like to add a node in a certain level as child. For example I know that with this code I can add a level one node plus a level two node: ...
11
by: ctman770 | last post by:
Hi Everyone, Is it faster to save the precise location of an html dom node into a variable in js, or to use getElementById everytime you need to access the node? I want to make my application...
6
by: Derek Hart | last post by:
I bring in an xml file into vb.net by using xmlDoc.LoadXml(XMLString) - I run xpath statements against the xml file to grab data from it, so I use, as an example, //Vehicles/Vehicles/@make to get...
10
by: Simon Brooke | last post by:
The DOM API has included public Node importNode(Node,boolean) as a method of the Document interface for a long time. Does anything actually implement it? Xerces 2 is giving me: ...
2
by: Dahak | last post by:
It seems that my GoogleFu has failed me tonight and I'm hoping I can find some advice. I've inherited a ASP.NET project written in Visual Basic. I'm not too familiar with the .NET framework...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.