Hello,
I'm experimenting with and XmlTextReader sample, and don't understand how
the attributes are being processed.
Here's the input:
<xsd:element name="AA" type="BB" substitutionGroup="CC"/>
The code:
while (reader->Read())
{
switch (reader->NodeType)
{
// snip cases
case XmlNodeType::Element:
/*A*/ Format (reader, S"Element");
while (reader->MoveToNextAttribute())
{
/*B*/ Format (reader, S"Attribute");
}
elementCount++;
if (reader->HasAttributes)
attributeCount += reader->AttributeCount;
break;
// snip cases
}
}
// Format the output
void Format(XmlReader* reader, String* NodeType)
{
// Format the output
Console::Write(String::Concat(__box(reader->Depth), S" "));
Console::Write(String::Concat(__box(reader->AttributeCount), S" "));
for (int i=0; i < reader->Depth; i++)
{
Console::Write(S"\t");
}
/*D*/ Console::Write(String::Concat(NodeType, S"<", reader->Name, S">",
reader->Value));
// Display the attributes values for the current node
if (reader->HasAttributes)
{
Console::Write(" Attributes:");
/*C*/ for (int j=0; j < reader->AttributeCount; j++)
{
Console::Write(String::Concat(S" [" , __box(j) , S"] " ,
reader->Item[j]));
}
}
Console::WriteLine();
}
And the output:
1 3 Element<xsd:element> Attributes: [0] AA [1] BB [2] CC
2 3 Attribute<name>AA Attributes: [0] AA [1] BB [2] CC
2 3 Attribute<type>BB Attributes: [0] AA [1] BB [2] CC
2 3 Attribute<substitutionGroup>CC Attributes: [0] AA [1] BB [2]
CC
The first call to Format(), reader is at the xsd:element, and Format()
iterates the attributes and prints their values (at C).
When Format() is called at B, reader is at <name>. Line D correctly prints
the attributes name and value (reader->Name, reader->Value).
However, when we get to the line marked C, all the attributes for the
element are enumerated, as though reader were still at <xsd:element>.
What is going on here?
Thanks for any insight.
C.