Newbie to XML

S

Sorin Sandu

It's my first time when I use XML and it's not working.
I have the following code.

WindowsIdentity MyIdentity = WindowsIdentity.GetCurrent();
string IdentName = MyIdentity.Name;
string suser = IdentName.ToString().Substring(4);
XmlTextReader reader = null;
reader = new XmlTextReader("f:\\intranet\\users.xml");

while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
if (reader.Name == "name") // After this reader.ReadString() = "" ????
{
string test = reader.ReadString(); // Here is not working
if (test.ToUpper() == suser.ToUpper())
{
string mylevel = reader.GetAttribute("level");
Label1.Text = mylevel;
}

}
}
}
reader.Close();
And this is my xml
<?xml version="1.0" encoding="utf-8" ?>
<users>
<name level='0' >ASPNET</name>
</users>
What's wrong with this code ???
 
M

Martin Honnen

Sorin said:
It's my first time when I use XML and it's not working.
I have the following code.
reader = new XmlTextReader("f:\\intranet\\users.xml");

while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
if (reader.Name == "name") // After this reader.ReadString() = "" ????
{
string test = reader.ReadString(); // Here is not working
if (test.ToUpper() == suser.ToUpper())
{
string mylevel = reader.GetAttribute("level");
Label1.Text = mylevel;
}

}
}
}
reader.Close();
And this is my xml
<?xml version="1.0" encoding="utf-8" ?>
<users>
<name level='0' >ASPNET</name>
</users>
What's wrong with this code ???

I guess the problem is that you first try to read the content of the
element and then the attribute, try to read the attribute first and then
read the content of the element, using your XML above and the following C#

XmlTextReader xmlReader = new XmlTextReader(@"test2004073004.xml");
while (xmlReader.Read()) {
if (xmlReader.NodeType == XmlNodeType.Element) {
if (xmlReader.Name == "name") {
string level = xmlReader.GetAttribute("level");
string userName = xmlReader.ReadString();
if (userName == "ASPNET") {
Console.WriteLine(level);
}
}
}
}

I have no problem to output the level.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top