Groups | Blog | Home
all groups > dotnet xml > may 2004 >

dotnet xml : Question about attributes


Developer
5/25/2004 12:47:23 PM
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.

Developer
5/26/2004 6:41:05 AM
That solves my problem, but it is confusing behavior...
Thanks for the reply.

[quoted text, click to view]

v-kevy NO[at]SPAM online.microsoft.com
5/26/2004 8:17:40 AM
Hi Developer,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that when you have moved the XmlTextReader
to the next attribute, the HasAttributes property still returns true. If
there is any misunderstanding, please feel free to let me know.

Thanks for your repro code. Based on my research, this is by design.
HasAttribute property still returns true when the node type is attribute.
To workaround this, we can check if the node type is element before working
into attributes. Here I have made made some changes to your code. HTH.

if (reader->HasAttributes && reader->NodeType == XmlNodeType::Element)
{
Console::Write(" Attributes:");

/*C*/ for (int j=0; j < reader->AttributeCount; j++)
{
Console::Write(String::Concat(S" [" , __box(j) , S"] " ,
reader->Item[j]));
}
}

If anything is unclear, please feel free to reply to the post.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
yhhuang NO[at]SPAM online.microsoft.com
6/3/2004 3:27:23 AM
Hi,

Generally speaking, when reading xml files, the correct steps are to read node first, for each node, use MoveToNExtAttribute() to read its
attributes.

Attribute nodes are not automatically visited by a reader that moves forward with the Read method. To visit the set of attributes of the current
element node you must use a similar loop, but one that is controlled by the MoveToNextAttribute method. The following code accesses all
the attributes of the current node (the one you selected with Read) and concatenates their names and values into a comma-separated string:
if (reader.HasAttributes)
while(reader.MoveToNextAttribute())
buf += reader.Name + "=\"" + reader.Value + "\",";
reader.MoveToElement();

Please refer to the following articles:
"HOW TO: Read XML from a File by Using Visual C# .NET"
http://support.microsoft.com/?id=307548
"XML in .NET: .NET Framework XML Classes and C# Offer Simple, Scalable Data Manipulation"
http://msdn.microsoft.com/msdnmag/issues/01/01/xml/default.aspx

Hope that helps.

Best regards,
Yanhong Huang
Microsoft Community Support

Get Secure! ¨C www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
Developer
6/3/2004 8:42:03 AM
Thanks, those links are helpful.

[quoted text, click to view]

AddThis Social Bookmark Button