Connecting Tech Pros Worldwide Forums | Help | Site Map

C# Read XML - Need help please!

Chua Wen Ching
Guest
 
Posts: n/a
#1: Nov 16 '05
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 />
<EstablishDevice>
<DeviceName>Titan 12</DeviceName>
<DeviceLength>8</DeviceLength>
</EstablishDevice>
<Reset>
<Slot>0</Slot>
</Reset>
</Device>
<Device Type="A2">
<EstablishDevice>
<DeviceName>Titan 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 />
<EstablishDevice Name="Titan 12" Length="8" />
<Reset Slot="0" />
</Device>
<Device Type="A2">
<Open />
<EstablishDevice 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(strPath);

// Create instance of MyLibrary
_library = new MyLibrary();

string sName = string.Empty;

while (xmlreader.Read())
{
switch (xmlreader.NodeType)
{
case XmlNodeType.Element:
//
//sName = xmlreader.Name;
if (xmlreader.Name.Equals("Device"))
{
lstResult.Items.Add("Type of Reader: " +
xmlreader.GetAttribute("Type"));
lstResult.Items.Add("Build in progress ...");
}


if (xmlreader.Name.Equals("Open"))
{
try
{
// MyLibrary Open Method
_library.Open();
lstResult.Items.Add("OpenDevice is success");
}
catch (Exception e)
{
lstResult.Items.Add("Open is not success" + re.Message);
}
}

if (xmlreader.Name.Equals("EstablishDevice"))
{
string deviceName = xmlreader.GetAttribute("Name");
int deviceLength = Int32.Parse(xmlreader.GetAttribute("Length"));

try
{
// Establish hardware connection
_library.EstablishDevice(deviceName, deviceLength);
lstResult.Items.Add("EstablishDevice " +
xmlreader.GetAttribute("Name") + " " + xmlreader.GetAttribute("Length") + "
is successfull!");
}
catch (Exception e)
{
lstResult.Items.Add("Error: " + e.Message);
}
}

if (xmlreader.Name.Equals("Reset"))
{
short slotNumber = Int16.Parse(xmlreader.GetAttribute("Slot"));

try
{
// Reset the hardware before used
_library.Reset(slotNumber);
lstResult.Items.Add("Reset Device at Slot " +
xmlreader.GetAttribute("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>12345678</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

Dennis Myrén
Guest
 
Posts: n/a
#2: Nov 16 '05

re: C# Read XML - Need help please!


I would recommend using DOM(XmlDocument) if the document is not huge.
Because it is easier, especially in this context of reading special nodes.
XmlDocument doc = new XmlDocument();
doc.Load(strPath);
To get the first "DataIn" element:
// ASSUMING "Command" is direct descendant of root.
XmlNode node =
doc.DocumentElement.SelectSingleNode("Command[@id='A001']/Slot/DataIn");
Console.WriteLine(node.InnerText);

If you need more help, please explain exactly what you are trying to do.

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"Chua Wen Ching" <chua_wen_ching@nospam.hotmail.com> wrote in message
news:109A630D-3807-4F87-B3E2-D1F07ED3E635@microsoft.com...[color=blue]
> 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 />
> <EstablishDevice>
> <DeviceName>Titan 12</DeviceName>
> <DeviceLength>8</DeviceLength>
> </EstablishDevice>
> <Reset>
> <Slot>0</Slot>
> </Reset>
> </Device>
> <Device Type="A2">
> <EstablishDevice>
> <DeviceName>Titan 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 />
> <EstablishDevice Name="Titan 12" Length="8" />
> <Reset Slot="0" />
> </Device>
> <Device Type="A2">
> <Open />
> <EstablishDevice 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(strPath);
>
> // Create instance of MyLibrary
> _library = new MyLibrary();
>
> string sName = string.Empty;
>
> while (xmlreader.Read())
> {
> switch (xmlreader.NodeType)
> {
> case XmlNodeType.Element:
> //
> //sName = xmlreader.Name;
> if (xmlreader.Name.Equals("Device"))
> {
> lstResult.Items.Add("Type of Reader: " +
> xmlreader.GetAttribute("Type"));
> lstResult.Items.Add("Build in progress ...");
> }
>
>
> if (xmlreader.Name.Equals("Open"))
> {
> try
> {
> // MyLibrary Open Method
> _library.Open();
> lstResult.Items.Add("OpenDevice is success");
> }
> catch (Exception e)
> {
> lstResult.Items.Add("Open is not success" + re.Message);
> }
> }
>
> if (xmlreader.Name.Equals("EstablishDevice"))
> {
> string deviceName = xmlreader.GetAttribute("Name");
> int deviceLength = Int32.Parse(xmlreader.GetAttribute("Length"));
>
> try
> {
> // Establish hardware connection
> _library.EstablishDevice(deviceName, deviceLength);
> lstResult.Items.Add("EstablishDevice " +
> xmlreader.GetAttribute("Name") + " " + xmlreader.GetAttribute("Length") +
> "
> is successfull!");
> }
> catch (Exception e)
> {
> lstResult.Items.Add("Error: " + e.Message);
> }
> }
>
> if (xmlreader.Name.Equals("Reset"))
> {
> short slotNumber = Int16.Parse(xmlreader.GetAttribute("Slot"));
>
> try
> {
> // Reset the hardware before used
> _library.Reset(slotNumber);
> lstResult.Items.Add("Reset Device at Slot " +
> xmlreader.GetAttribute("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>12345678</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[/color]


Closed Thread