5/6/2010
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
namespace AaronSoftware.XML.Parser
{
public class WmlParser
{
public delegate void OnNewWmlInformationEventHandler(iWmlInfo info);
public event OnNewWmlInformationEventHandler OnNewWmlInformation;
public WmlParser()
{
}
public Boolean RequestData(String url)
{
try
{
XmlDocument _doc = new XmlDocument();
_doc.Load(url);
foreach (XmlNode node in _doc.ChildNodes)
{
foreach (XmlNode childNode in node.ChildNodes)
{
parseText(childNode.InnerText);
}
}
return true;
}
catch (Exception)
{
return false;
}
}
private void parseText(String text)
{
wmlInfo _wmlInfo = new wmlInfo();
//split up the values as char returns
String[] _split = text.Split('\n');
int loopCount = 0;
//loop through all the items in the split
foreach (String item in _split)
{
//look for the voltage in
if (item.ToLower().StartsWith("vin:"))
{
String[] _splitVoltage = item.ToLower().Split(':');
if (_splitVoltage.Length > 0)
{
String _voltage = _splitVoltage[1];
Console.WriteLine("Voltage in: " + _voltage);
_wmlInfo.Vin = _voltage;
}
}
else if (item.ToLower().StartsWith("vout:"))
{
String[] _splitVoltage = item.ToLower().Split(':');
if (_splitVoltage.Length > 0)
{
String _voltageOut = _splitVoltage[1];
Console.WriteLine("Voltage out: " + _voltageOut);
_wmlInfo.VOut = _voltageOut;
}
}
else if (item.ToLower().StartsWith("freq:"))
{
String[] _splitfreq = item.ToLower().Split(':');
if (_splitfreq.Length > 0)
{
String _freqOut = _splitfreq[1];
Console.WriteLine("freq out: " + _freqOut);
_wmlInfo.Freq = _freqOut;
}
}
else if (item.ToLower().StartsWith("load:"))
{
String[] _splitLoad = item.ToLower().Split(':');
if (_splitLoad.Length > 0)
{
String _load = _splitLoad[1];
Console.WriteLine("load: " + _load);
_wmlInfo.Load = _load;
}
}
else if (item.ToLower().StartsWith("batt cap:"))
{
String[] _splitBattCap = item.ToLower().Split(':');
if (_splitBattCap.Length > 0)
{
String _BattCap = _splitBattCap[1];
Console.WriteLine("Batt Cap: " + _BattCap);
_wmlInfo.BattCap = _BattCap;
}
}
else
{
if (loopCount == 0)
{
Console.WriteLine("Status: " + item);
_wmlInfo.Status = item;
}
}
loopCount += 1;
}
if (OnNewWmlInformation != null)
{
OnNewWmlInformation(_wmlInfo);
}
}
}
public class wmlInfo : iWmlInfo
{
private string _vIn = "";
private string _vOut = "";
private string _freq = "";
private string _load = "";
private string _battCap = "";
private string _status = "";
public String Vin
{
get { return _vIn; }
set { _vIn = value; }
}
public String VOut
{
get { return _vOut; }
set { _vOut = value; }
}
public String Freq
{
get { return _freq; }
set { _freq = value; }
}
public String Load
{
get { return _load; }
set { _load = value; }
}
public String BattCap
{
get { return _battCap; }
set { _battCap = value; }
}
public String Status
{
get { return _status; }
set { _status = value; }
}
}
public interface iWmlInfo
{
String Vin
{
get;
}
String VOut
{
get;
}
String Freq
{
get;
}
String Load
{
get;
}
String BattCap
{
get;
}
String Status
{
get;
}
}
}
From your code you now can call the following:
private void button2_Click(object sender, EventArgs e)
{
_wp.OnNewWmlInformation += new WmlParser.OnNewWmlInformationEventHandler(_wp_OnNewWmlInformation);
_wp.RequestData("http://10.0.220.1/stat.wml");
}
void _wp_OnNewWmlInformation(iWmlInfo info)
{
MessageBox.Show(info.BattCap);
}
Aaron Software takes no responsibility for the implementation of this shared code.