Xml Node Access

  • Thread starter Thread starter sitemap
  • Start date Start date
S

sitemap

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.Attributes;
if (xmlnode.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
 
sitemap said:
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.Attributes;
if (xmlnode.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 to XmlElement,
then invoke GetElementsByTagName on that, rather than on xmldoc.

Or you could just use XPath on the original document, couldn't you?
 
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 said:
sitemap said:
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.Attributes;
if (xmlnode.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 to XmlElement,
then invoke GetElementsByTagName on that, rather than on xmldoc.

Or you could just use XPath on the original document, couldn't you?
 
sitemap said:
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).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.
 
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.Attributes;
if (xmlnode.Attributes[0].Value.ToString() ==
ind.ToString())
{
_a = xmlnode.ChildNodes[1].Attributes;
}
}
mainForm.xmldoc = null;
return _a;
}

Thank you again.
Niky


Larry said:
sitemap said:
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).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.
 
Back
Top