C# and XmlTextReader.GetAttribute bug?

  • Thread starter Thread starter Willie B. Hardigan
  • Start date Start date
W

Willie B. Hardigan

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 = 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?
 
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
 
ok so if i close it in a..

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

}

thats ok
 
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 said:
ok so if i close it in a..

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

}

thats ok
yet
 
Back
Top