Hi there,
I have some problems when reading XML file.
1. First this, is what i did, cause i can't seem to read "sub elements or
tags" values, so i place those values into attributes like this.
Before:
----------
<?xml version="1.0" encoding="utf-8"?>
<MyConfig>
<Device Type="A1">
<Open />
<EstablishDevic e>
<DeviceName>Tit an 12</DeviceName>
<DeviceLength>8 </DeviceLength>
</EstablishDevice >
<Reset>
<Slot>0</Slot>
</Reset>
</Device>
<Device Type="A2">
<EstablishDevic e>
<DeviceName>Tit an 13</DeviceName>
<DeviceLength>8 </DeviceLength>
</EstablishDevice >
<Reset>
<Slot>1</Slot>
</Device>
</MyConfig>
After:
--------
<?xml version="1.0" encoding="utf-8"?>
<MyConfig>
<Device Type="A1">
<Open />
<EstablishDevic e Name="Titan 12" Length="8" />
<Reset Slot="0" />
</Device>
<Device Type="A2">
<Open />
<EstablishDevic e Name="Titan 13" Length="8" />
<Reset Slot="1" />
</Device>
</MyConfig>
So this is how my C# codes look like (but i think it is quite of wrong!)
C# - Read XML Tag + Attributes + Processing:
------------------------------------------------------
// strpath is the location of the xml file
XmlTextReader xmlreader = new XmlTextReader(s trPath);
// Create instance of MyLibrary
_library = new MyLibrary();
string sName = string.Empty;
while (xmlreader.Read ())
{
switch (xmlreader.Node Type)
{
case XmlNodeType.Ele ment:
//
//sName = xmlreader.Name;
if (xmlreader.Name .Equals("Device "))
{
lstResult.Items .Add("Type of Reader: " +
xmlreader.GetAt tribute("Type") );
lstResult.Items .Add("Build in progress ...");
}
if (xmlreader.Name .Equals("Open") )
{
try
{
// MyLibrary Open Method
_library.Open() ;
lstResult.Items .Add("OpenDevic e is success");
}
catch (Exception e)
{
lstResult.Items .Add("Open is not success" + re.Message);
}
}
if (xmlreader.Name .Equals("Establ ishDevice"))
{
string deviceName = xmlreader.GetAt tribute("Name") ;
int deviceLength = Int32.Parse(xml reader.GetAttri bute("Length")) ;
try
{
// Establish hardware connection
_library.Establ ishDevice(devic eName, deviceLength);
lstResult.Items .Add("Establish Device " +
xmlreader.GetAt tribute("Name") + " " + xmlreader.GetAt tribute("Length ") + "
is successfull!");
}
catch (Exception e)
{
lstResult.Items .Add("Error: " + e.Message);
}
}
if (xmlreader.Name .Equals("Reset" ))
{
short slotNumber = Int16.Parse(xml reader.GetAttri bute("Slot"));
try
{
// Reset the hardware before used
_library.Reset( slotNumber);
lstResult.Items .Add("Reset Device at Slot " +
xmlreader.GetAt tribute("Slot") + " is successfull!");
}
catch (Exception e)
{
lstResult.Items .Add("Error: " + e.Message);
}
}
break;
}
}
}
I really felt weird with the way i do. Is XPath a better technique to get
values directly? Just say, if i have 1000 lines of xml code, it is going to
read line by line even it does not use it. You get my meaning!
Now is my problem. I want to test my data which looks like below (just want
to avoid attributes for a while).
Assuming:
.....
.....
<Reset Slot="1" />
<!-- Add-on -->
</Device>
<Command id="A001">
<Slot Type="0">
<DataIn>1234567 8</DataIn>
<DataOut />
</Slot>
</Command>
<Command id="A002">
<Slot Type="0">
<DataIn />
<!-- Expect data out to be written here -->
<DataOut></DataOut>
</Slot>
</Command>
....
....
</MyConfig>
So now how do i read those values from DataIn?
I need to place some identification at Command, so i can know which command
tag to write the dataout.
Any help please? I hope you understand what is the problem here. If you
think there is a better way, please let me know!
Thanks.
--
Regards,
Chua Wen Ching
Visit us at http://www.necoders.com