473,406 Members | 2,843 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

C# and XmlTextReader.GetAttribute bug?



I have an XML file like so..

<?xml version="1.0" encoding="utf-8" ?>
<zap>
<data><string>abc</string><hex>a110ff</hex><rnd min="0"
max="10"></rnd><string>xyz</string></data>
</zap>
and i have a function that reads it as follows...

//-------------------------------------------------------------------------
private static void runFile (
string dataFile,
string ip,
int port
)

//-------------------------------------------------------------------------
// XML Format for patterns
//
// <?xml version="1.0" encoding="utf-8" ?>
// <zap>
// <data><string>abc</string><hex>a110ff</hex><rnd min="0"
max="10"></rnd><string>xyz</string></data>
// </zap>

//-------------------------------------------------------------------------
{
FileStream fs = new FileStream(dataFile, FileMode.Open);
StreamReader sr = new StreamReader(fs);
XmlTextReader xtr = new XmlTextReader(sr);
MemoryStream ms = new MemoryStream();
try
{
while (xtr.Read())
{
switch (xtr.Name)
{
case "data":
{
while (xtr.Read())
{
switch (xtr.Name)
{
case "string":
{
string s = xtr.ReadString();
ms.Capacity += s.Length;
ms.Write(convertStringToByteArray(s), 0, s.Length);
break;
}

case "hex":
{
string s = xtr.ReadString();
int len = s.Length/2;
byte[] ba = new byte[len];
for (int i = 0, j = 0 ; i <= len-1 ; i++, j+=2)
{
string sc = s[j].ToString();
sc += s[j+1].ToString();
int h = Convert.ToInt32(sc, 16);
ba[i] = Convert.ToByte(h);
}
ms.Capacity += len;
ms.Write(ba, 0, len);
break;
}

case "rnd":
{
int min = Int32.Parse(xtr.GetAttribute("min"));
int max = Int32.Parse(xtr.GetAttribute("max"));
ms.Capacity += max;
byte[] rndBa = new Byte[max];
rndBa = RndClass.RndBytes(min, max);
ms.Write(rndBa, 0, max);
break;
}

default:
{
break;
}
}
}
sendData(ip, port, ms.GetBuffer(), false, true);
break;
}

default:
{
break;
}
}
}
}

catch(Exception err)
{
Console.WriteLine(err.Message);
}
}

//-------------------------------------------------------------------------

I end up in the case "rnd": for the </rnd> end node for some reason yet when
i change the XML file to this...

<rnd min="0" max="10"/>

with no </rnd> it works fine.

What gives?

--

Willie B. Hardigan
Microsoft Product Deactivation Team
--

Nov 15 '05 #1
3 6885
Willie B. Hardigan <Wi***************@microsooft.com> wrote:

<snip>
I end up in the case "rnd": for the </rnd> end node for some reason yet when
i change the XML file to this...

<rnd min="0" max="10"/>

with no </rnd> it works fine.

What gives?


XmlTextReader.Name gives the name of the node, which is rnd whether the
node type is element or end element. Nothing strange about that - you
need to change your code to skip over nodes which aren't element nodes.

Here's a quick program which shows you what's going on:
using System;
using System.Xml;
using System.IO;

public class Test
{
static void Main()
{
StringReader sr = new StringReader (
@"<?xml version=""1.0"" encoding=""utf-8"" ?>
<zap>
<data><string>abc</string><hex>a110ff</hex><rnd min=""0""
max=""10""></rnd><string>xyz</string></data>
</zap>");

XmlTextReader xtr = new XmlTextReader(sr);

while (xtr.Read())
{
Console.WriteLine ("{0,-10} {1}", xtr.NodeType, xtr.Name);
}
}
}

It produces output of:
XmlDeclaration xml
Whitespace
Element zap
Whitespace
Element data
Element string
Text
EndElement string
Element hex
Text
EndElement hex
Element rnd
EndElement rnd
Element string
Text
EndElement string
EndElement data
Whitespace
EndElement zap

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #2
ok so if i close it in a..

if (xtr.IsStartElement() == true)
{
switch (xtr.Name)
{
case "string":
..
...

}

thats ok
--

Willie B. Hardigan
Microsoft Product Deactivation Team
--
"Jon Skeet" <sk***@pobox.com> wrote in message
news:MP************************@news.microsoft.com ...
Willie B. Hardigan <Wi***************@microsooft.com> wrote:

<snip>
I end up in the case "rnd": for the </rnd> end node for some reason yet when i change the XML file to this...

<rnd min="0" max="10"/>

with no </rnd> it works fine.

What gives?


XmlTextReader.Name gives the name of the node, which is rnd whether the
node type is element or end element. Nothing strange about that - you
need to change your code to skip over nodes which aren't element nodes.

Here's a quick program which shows you what's going on:
using System;
using System.Xml;
using System.IO;

public class Test
{
static void Main()
{
StringReader sr = new StringReader (
@"<?xml version=""1.0"" encoding=""utf-8"" ?>
<zap>
<data><string>abc</string><hex>a110ff</hex><rnd min=""0""
max=""10""></rnd><string>xyz</string></data>
</zap>");

XmlTextReader xtr = new XmlTextReader(sr);

while (xtr.Read())
{
Console.WriteLine ("{0,-10} {1}", xtr.NodeType, xtr.Name);
}
}
}

It produces output of:
XmlDeclaration xml
Whitespace
Element zap
Whitespace
Element data
Element string
Text
EndElement string
Element hex
Text
EndElement hex
Element rnd
EndElement rnd
Element string
Text
EndElement string
EndElement data
Whitespace
EndElement zap

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too

Nov 15 '05 #3
hehe i just send the word "ASS" in hex to a wap gateway and it crashes on
9200 :D

<?xml version="1.0" encoding="utf-8" ?>
<zap>
<data><string>ass</string></data>
</zap>
D:\>zapper z.xml 127.0.0.1 9200
0x61 0x73 0x73 = A S S
I gotta run a rude word against that port just for piss taking and publicise
the DoS

:D
--

Willie B. Hardigan
Microsoft Product Deactivation Team
--
"Willie B. Hardigan" <Wi***************@microsooft.com> wrote in message
news:eZ**************@TK2MSFTNGP10.phx.gbl...
ok so if i close it in a..

if (xtr.IsStartElement() == true)
{
switch (xtr.Name)
{
case "string":
..
...

}

thats ok
--

Willie B. Hardigan
Microsoft Product Deactivation Team
--
"Jon Skeet" <sk***@pobox.com> wrote in message
news:MP************************@news.microsoft.com ...
Willie B. Hardigan <Wi***************@microsooft.com> wrote:

<snip>
I end up in the case "rnd": for the </rnd> end node for some reason
yet
when i change the XML file to this...

<rnd min="0" max="10"/>

with no </rnd> it works fine.

What gives?


XmlTextReader.Name gives the name of the node, which is rnd whether the
node type is element or end element. Nothing strange about that - you
need to change your code to skip over nodes which aren't element nodes.

Here's a quick program which shows you what's going on:
using System;
using System.Xml;
using System.IO;

public class Test
{
static void Main()
{
StringReader sr = new StringReader (
@"<?xml version=""1.0"" encoding=""utf-8"" ?>
<zap>
<data><string>abc</string><hex>a110ff</hex><rnd min=""0""
max=""10""></rnd><string>xyz</string></data>
</zap>");

XmlTextReader xtr = new XmlTextReader(sr);

while (xtr.Read())
{
Console.WriteLine ("{0,-10} {1}", xtr.NodeType, xtr.Name);
}
}
}

It produces output of:
XmlDeclaration xml
Whitespace
Element zap
Whitespace
Element data
Element string
Text
EndElement string
Element hex
Text
EndElement hex
Element rnd
EndElement rnd
Element string
Text
EndElement string
EndElement data
Whitespace
EndElement zap

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too


Nov 15 '05 #4

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

Similar topics

1
by: Jesper Stocholm | last post by:
I have som XML like this: <root> <Course CourseCode="id1"> <Teacher Name="Some name"/> <Title Titlde="Dansk Titel 1"/> <Title Title="English Title 1"/> <Location Place="Some place"/>...
1
by: Gustaf Liljegren | last post by:
I shall make a make a function that will read an XML file, looking for elements like this <context id="A1"> <period> <instant>2001-01-01</instant> </period> </context> or this
5
by: Geoff Bennett | last post by:
While parsing an XML document, my TextReader instance skips nodes. For example, in this fragment: <Person Sex="Male" FirstHomeBuyer="No" YearsInCurrentProfession="14"> <RelatedEntityRef...
1
by: soupaman | last post by:
Im trying to output some filtered xml using the xmlTextReader I know the code and commenting needs cleaned up and eventually plan to add the values to a dataset. currently what this is doing is...
1
by: Dica | last post by:
hi all first off, i'm not trying to cross post, but couldn't find this newsgroup earlier (got here from a recommendation on microsoft.public.vb, where i originally posted this question). ...
1
by: Gustaf | last post by:
My docs have attributes called "order", with a default value of 1. So if there is no "order" attribute, the program shall use the value 1. Implementing this with XmlTextReader was harder than...
0
by: edamron | last post by:
I wish to use a simple xml file as a configuration file for a small application. The application writes a default xml file if it doesn't find the configuration file but when it reads the file it...
1
by: lejason | last post by:
Hi, I have an XML file that is a list of product models and info. The list will eventually contain about 100 products, each product having multiple elements for things like names,...
4
by: RobG | last post by:
I have always accessed attributes such as disabled using the DOM element property, however I was wondering about implementing a more generic function to get the values of attributes - which of...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.