472,371 Members | 1,668 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,371 software developers and data experts.

C# Read XML - Need help please!

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
Nov 16 '05 #1
1 3256
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" <ch************@nospam.hotmail.com> wrote in message
news:10**********************************@microsof t.com...
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

Nov 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Shaun Merrill | last post by:
Hey, here is my code. I am trying to read a file into a byte array. Please show me how this can work. Obviously I need to read into a dynamic array because it could read any file. So why does...
3
by: Udhay | last post by:
Sir, I want to read the data of an audio file in c++ (Windows). What is the API which helps to read the data of an audio file. I want to read BitRate,Audio Sample Size,Audio Sample rate and...
5
by: =?Utf-8?B?bXBhaW5l?= | last post by:
Hello, I am completely lost as to why I can't update a DropDownList inside a DetailsView after I perform an insert into an object datasource. I tried to simply it down to the core demostration:...
3
by: =?Utf-8?B?c2lwcHl1Y29ubg==?= | last post by:
Hi I have an xsd that I want to save as an XML string to store in a DB I can save as a physical file using xsd.WriteXml(@"C:\Temp\Junk\junk.xml"); But I am unable to save to a string so I...
2
by: =?Utf-8?B?c2lwcHl1Y29ubg==?= | last post by:
Hi Is there any way to Read an INIFile from a string or Stream instead of a physical file ??? I want to read the INIFile into a string then store in a db but when I read the string from the...
3
by: Laura | last post by:
Hi: ¿How can I read a file on the client side without loading it to the server?
2
by: Riak | last post by:
Hellow Helpers: I am doing data entry job manually and data is huge. Now I was told to write/get some sort of program/script which can do this job quickly, otherwise I will loose job. I am good...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.
0
DizelArs
by: DizelArs | last post by:
Hi all) Faced with a problem, element.click() event doesn't work in Safari browser. Tried various tricks like emulating touch event through a function: let clickEvent = new Event('click', {...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.