473,405 Members | 2,185 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,405 software developers and data experts.

XmlBookmarkReader bug using ReadSubtree

I have discovered a bug in XmlBookmarkReader (code provided by MSDN,
but not necessarily supported by Microsoft). The article is found
here:
http://msdn.microsoft.com/library/de...mlBkMkRead.asp

Consider the following code to print xml. This code assumes that the
xml text given in the reader will be of a certain form -- basically
having a root node, which is not a crazy assumption at all. It just
makes the code simpler to demonstrate the bug.

public static void PrintXml(XmlReader reader)
{
bool inElement = false;
string elementName="";

XmlBookmarkReader bReader = new XmlBookmarkReader(reader);

bReader.Read();
if (bReader.HasAttributes)
{
Console.Write("<"+bReader.Name);
for (int i=0; i < bReader.AttributeCount; i++)
{
Console.Write(" ");
bReader.MoveToAttribute(i);
Console.Write("{0}=\"{1}\"", bReader.Name, bReader.Value);
}
bReader.MoveToElement();
Console.WriteLine(">");
}
else
{
Console.WriteLine("<"+bReader.Name+">");
}

while (bReader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element:
if (inElement)
{
bReader.ReturnToBookmark(elementName);
PrintXml(bReader.ReadSubtree());
inElement = false;
}
else
{
inElement = true;
elementName = bReader.Name;
bReader.SetBookmark(elementName);
if (!bReader.HasAttributes)
Console.Write("<" + bReader.Name + ">");
else
{
Console.Write("<" + bReader.Name);
for (int i = 0; i < bReader.AttributeCount; i++)
{
Console.Write(" ");
bReader.MoveToAttribute(i);
Console.Write("{0}=\"{1}\"", bReader.Name,
bReader.Value);
}
bReader.MoveToElement();
Console.Write(">");
}
}
break;
case XmlNodeType.EndElement:
Console.WriteLine("</" + bReader.Name + ">");
inElement = false;
bReader.RemoveBookmark(elementName);
break;
case XmlNodeType.Text:
Console.Write(bReader.Value);
break;
}
}
reader.Close();
}

Now pass the code above this XML text:
<Config>
<Id attrib="false">12345-6789-12364</Id>
<DisplayName attrib="true">Computer</DisplayName>
<ServerList count="4">
<Server0 checkedin="true">https://web1.com</Server0>
<Server1 checkedin="false">https://web2.com</Server1>
<Server2 checkedin="false">https://web3.com</Server2>
<Server3 checkedin="false">https://web4.com</Server3>
</ServerList>
</Config>

Code to turn that text into an XmlReader:
string xml = [XML text above];
XmlNamespaceManager nsmgr = new XmlNamespaceManager(new NameTable());
nsmgr.AddNamespace("", "");
XmlParserContext parseContext =
new XmlParserContext(null, nsmgr, null, XmlSpace.None);
XmlTextReader reader =
new XmlTextReader(xml, XmlNodeType.Document, parseContext);
reader.WhitespaceHandling = WhitespaceHandling.None;
PrintXml(reader);

You get this result:
<Config>
<Id attrib="false">12345-6789-12364</Id>
<DisplayName attrib="true">Computer</DisplayName>
<ServerList count="4"><ServerList count="4">
<Server0 checkedin="true">https://web1.com</Server0>
<Server0 checkedin="true">https://web2.com</Server1>
<Server0 checkedin="true">https://web3.com</Server2>
<Server0 checkedin="true">https://web4.com</Server3>
</ServerList>
</Config>

Notice the Server1, Server2 and Server3 start elements all are Server0,
but the end elements are correct. Notice they all share the same
'checkedin' attribute values as Server0 as well. ServerList appearing
twice is a side effect of having to go back using the bookmark. That
can be dealt with, but for simplicity, I didn't bother to correct it
here.

I've pinned this bug down to an issue with XmlBookmarkReader and
ReadSubtree. There are some strange effects going on that I don't
understand. For example, removing the "checkedin="true"" from the
Server0 element in the original XML doesn't result in this behavior.
You get the results you would expect. In fact, adding any
non-attributed element before Server0 gives you the results you would
expect even an empty element.

The only bit of insight I have with this problem is that the original
reader, before the recursive call to PrintXml, is positioned on Server0
while the bookmark reader is positioned on the cached ServerList node
when the call to ReadSubtree is executed.

Any help pinning down the problem would be greatly appreciated.

My conclusion is that a call to ReadSubtree on CachedXmlNode in the
XmlBookmarkReader may have unexpected results," but I would still like
to solve this problem.

Mar 17 '06 #1
1 3069
A solution, though not as elegant, is to forget about using
ReadSubtree. Instead you assume either the first node read or the
current node is the root node and you read until you encouter the
EndElement with the same name as the root node. You call PrintXml
recursively like above, but you must remove all bookmarks (because
you're working with the same XmlBookmarkReader).

It works, but has a lot more potential for error. If anyone is
interested, I can post the code.

Mar 17 '06 #2

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

Similar topics

2
by: rawCoder | last post by:
Hi All, I have a *.cer file, a public key of some one and I want to encrypt some thing using this public key. Can someone point me to a sample code for Encrypting some file using...
1
by: Mike | last post by:
When trying to compile (using Visual Web Developer 2005 Express Beta; frameworkv2.0.50215 ) the source code below I get errors (listed below due to the use of ICallBackEventHandler. Ultimately I...
10
by: Christopher Benson-Manica | last post by:
Why can't I use a class destructor in a using declaration: using MyClass::~MyClass; ? -- Christopher Benson-Manica | I *should* know what I'm talking about - if I ataru(at)cyberspace.org ...
17
by: beliavsky | last post by:
Many of my C++ programs have the line using namespace std; but the "Accelerated C++" book of Koenig and Moo has many examples where the library names are included one at a time, for example ...
8
by: Petter Reinholdtsen | last post by:
I ran into a problem on HP-UX 11.00 the other day, where it refused to compile a program using 'using namespace std;' at the top. The reason seem to be that the compiler refuses to accept 'using...
14
by: john.burton.email | last post by:
I've done some extensive searching and can't seem to find an answer to this - Is it correct to using "using" with templates, for example: using std::vector; Or do I need to specify the type...
5
by: Enos Meroka | last post by:
Hallo, I am a student doing my project in the university.. I have been trying to compile the program using HP -UX aCC compiler, however I keep on getting the following errors. ...
12
by: Calum Grant | last post by:
In older C++ computer books, you'll often see using namespace std; even in my 1996 copy of Stroustrup. Nowadays, it seems to be considered better to qualify names to make it clearer what...
1
by: Matt | last post by:
Hi everyone, I'm using the command mysqlcheck --all-databases --auto-repair --silent and I see these warnings (listed below) on a daily occurrence. Is mysqlcheck causing these warnings? I...
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
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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.