Click here to Skip to main content
16,015,218 members
Please Sign up or sign in to vote.
2.33/5 (2 votes)
See more:
<?xml version="1.0" ?><MotePacket><ParsedDataElement><Name>amtype</Name><ConvertedValue>11</ConvertedValue></ParsedDataElement><ParsedDataElement><Name>nodeid</Name><ConvertedValue>1</ConvertedValue></ParsedDataElement><ParsedDataElement><Name>parent</Name><ConvertedValue>0</ConvertedValue></ParsedDataElement><ParsedDataElement><Name>group</Name><ConvertedValue>125</ConvertedValue></ParsedDataElement><ParsedDataElement><Name>socketid</Name><ConvertedValue>51</ConvertedValue></ParsedDataElement><ParsedDataElement><Name>board_id</Name><ConvertedValue>132</ConvertedValue></ParsedDataElement><ParsedDataElement><Name>packet_id</Name><ConvertedValue>129</ConvertedValue></ParsedDataElement><ParsedDataElement><Name>voltage</Name><ConvertedValue>2636</ConvertedValue></ParsedDataElement><ParsedDataElement><Name>temp</Name><ConvertedValue>25.519222</ConvertedValue></ParsedDataElement><ParsedDataElement><Name>light</Name><ConvertedValue>1543</ConvertedValue></ParsedDataElement><ParsedDataElement><Name>mic</Name><ConvertedValue>313</ConvertedValue></ParsedDataElement><ParsedDataElement><Name>accel_x</Name><ConvertedValue>-1660.000000</ConvertedValue></ParsedDataElement><ParsedDataElement><Name>accel_y</Name><ConvertedValue>960.000000</ConvertedValue></ParsedDataElement><ParsedDataElement><Name>mag_x</Name><ConvertedValue>30.790493</ConvertedValue></ParsedDataElement><ParsedDataElement><Name>mag_y</Name><ConvertedValue>30.790493</ConvertedValue></ParsedDataElement></MotePacket>

<?xml version="1.0" ?><MotePacket><ParsedDataElement><Name>amtype</Name><ConvertedValue>11</ConvertedValue></ParsedDataElement><ParsedDataElement><Name>nodeid</Name><ConvertedValue>1</ConvertedValue></ParsedDataElement><ParsedDataElement><Name>parent</Name><ConvertedValue>0</ConvertedValue></ParsedDataElement><ParsedDataElement><Name>group</Name><ConvertedValue>125</ConvertedValue></ParsedDataElement><ParsedDataElement><Name>socketid</Name><ConvertedValue>51</ConvertedValue></ParsedDataElement><ParsedDataElement><Name>board_id</Name><ConvertedValue>132</ConvertedValue></ParsedDataElement><ParsedDataElement><Name>packet_id</Name><ConvertedValue>129</ConvertedValue></ParsedDataElement><ParsedDataElement><Name>voltage</Name><ConvertedValue>2614</ConvertedValue></ParsedDataElement><ParsedDataElement><Name>temp</Name><ConvertedValue>22.881853</ConvertedValue></ParsedDataElement><ParsedDataElement><Name>light</Name><ConvertedValue>1983</ConvertedValue></ParsedDataElement><ParsedDataElement><Name>mic</Name><ConvertedValue>285</ConvertedValue></ParsedDataElement><ParsedDataElement><Name>accel_x</Name><ConvertedValue>-80.000000</ConvertedValue></ParsedDataElement><ParsedDataElement><Name>accel_y</Name><ConvertedValue>520.000000</ConvertedValue></ParsedDataElement><ParsedDataElement><Name>mag_x</Name><ConvertedValue>30.925539</ConvertedValue></ParsedDataElement><ParsedDataElement><Name>mag_y</Name><ConvertedValue>30.790493</ConvertedValue></ParsedDataElement></MotePacket>


above are two xml files that i have append to one. the another part is to read the element name "light " and its converted values. i can read one light value along with its converted value. below is the code. but further task is to compare light values. in above file there are two differnt values for light one is 1543 and other id 1983. wht i want is that if the light increase the limit of 1600 it should give an alert text in my text box. plz suggest something. its last step of my project.

What I have tried:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using System.Xml.XPath;

namespace read_xml_elements
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
          
            XmlTextReader Reader = new XmlTextReader("C:\\Memsic\\cygwin\\home\\admin\\22.xml");
            XmlDocument doc = new XmlDocument();
            doc.Load(Reader);
            XPathNavigator nav = doc.CreateNavigator();
            
            //compile xpath 
             XPathExpression expr;

             expr = nav.Compile("/MotePacket/ParsedDataElement/[Name='light']");
            
             XPathNodeIterator iterator = nav.Select(expr);

            //iterate node set
            listBox1.Items.Clear();
            try
            {
                while (iterator.MoveNext())
                {
                    XPathNavigator nav2 = iterator.Current.Clone();
                    listBox1.Items.Add("Name: " + nav2.Value);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            
        }
    }
}
Posted
Updated 23-Aug-22 4:47am
Comments
George Swan 26-Jun-17 3:49am    
When you joined the two xml files you duplicated the declaration statement and root element. I suggest you correct this by removing the second declaration statement and adding a new root element. The XPathExpression ".//ParsedDataElement [Name='light']" should then enable you to extract the light values.
g.bhangu 26-Jun-17 19:31pm    
@George , i have tried that way and i knwo its working that way only but the problem is that i am unable to joint two xml files in one. so what i did is take load two xml in one by appending it. so at last when i open that newly saved file its also taking the root elements and declerations. so any idea how can joint two differnt xmls having same root and declearations in one, which just add the elements only?

1 solution

Something like this:

C#
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Windows.Forms;
using System.Xml.Linq;
...

XElement _x = XElement.Load(@"22.xml");

	var lightElements = from page in _x.Elements("ParsedDataElement")
			where page.Element("Name").Value == "light"
			select page;

	foreach (var item in lightElements)
	{
		listBox1.Items.Add(item.Element("ConvertedValue").Value);
	}
 
Share this answer
 
v2

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