Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to read the values of x,y,angle,direction,file

XML
<DESCIONTREE>
 
  <Motion X="296" Y="88" Angle="-90" Direction="up" file="2.jpg" />
  <Motion X="384" Y="94" Angle="90" Direction="down" file="2.jpg" />
  <Motion X="480" Y="94" Angle="90" Direction="down" file="2.jpg" />
  <Motion X="272" Y="106" Angle="90" Direction="down" file="2.jpg" />
 
</DESCIONTREE>


C#
string contents = File.ReadAllText("test.xml");
 
          XmlDocument xml = new XmlDocument();
            xml.LoadXml(contents);  // suppose that str string contains "<Names>...</Names>"
 
            XmlNodeList xnList = xml.SelectNodes("/DESCIONTREE/Motion");
 
            foreach (XmlNode xn in xnList)
            {
               // Console.WriteLine(xn.InnerText);
 
                richTextBox1.AppendText(xn.OuterXml+ "\n");
            }

but i want to store each variable

C#
String x,y,angle,direction,file ;
Posted
Updated 1-Jul-13 20:08pm
v2

You can access the attributes directly:
C#
XmlDocument xml = new XmlDocument();
xml.LoadXml(@"
<DESCIONTREE>
  <Motion X='296' Y='88' Angle='-90' Direction='up' file='2.jpg' />
  <Motion X='384' Y='94' Angle='90' Direction='down' file='2.jpg' />
</DESCIONTREE>
");

XmlNodeList xnList = xml.SelectNodes("/DESCIONTREE/Motion");


foreach (XmlNode xn in xnList)
{
    Console.WriteLine("{0} {1} {2} {3} {4}", xn.Attributes["X"].Value, xn.Attributes["Y"].Value, xn.Attributes["Angle"].Value, xn.Attributes["Direction"].Value, xn.Attributes["file"].Value);
}


But I suggest you declare a class, and use deserialization instead. Look here: http://undefinedvalue.com/2011/11/22/deserializing-objects-xml-c[^]
 
Share this answer
 
Comments
vishnulalr 2-Jul-13 2:37am    
thanks Zoltán Zörgő for your reply :-)
Hi,

you have read the attributes which you can achieve by following:


foreach (XmlNode xn in xnList)
{
string attributeValue = xn.Attributes["X"].Value; // to get the X value.
}

Hope this can solve your problem.

Regards
 
Share this answer
 
Comments
vishnulalr 2-Jul-13 2:38am    
thanks sowvin for your reply :-)
Hello Vishnulalr,

Following code snippet demonstrate how you can read individual attribute values from node.
C#
using System;
using System.Xml;

class XMLTest {
    public static void Main() {
        XmlDocument xdoc = new XmlDocument();
        xdoc.Load("G:\\text.xml");
        XmlNodeList xnLst = xdoc.SelectNodes("/DESCIONTREE/Motion");
        foreach (XmlNode node in xnLst) {
            Console.WriteLine(node.Attributes.GetNamedItem("X").Value);
            Console.WriteLine(node.Attributes.GetNamedItem("Y").Value);
            Console.WriteLine(node.Attributes.GetNamedItem("Angle").Value);
            Console.WriteLine(node.Attributes.GetNamedItem("Direction").Value);
            Console.WriteLine(node.Attributes.GetNamedItem("file").Value);
            Console.WriteLine("");
        }
        Console.ReadKey();
    }
}

Regards,
 
Share this answer
 
Comments
vishnulalr 2-Jul-13 2:38am    
thanks Prasad Khandekar for your reply :-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900