Newbie to XML

  • Thread starter Thread starter Sorin Sandu
  • Start date Start date
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 ???
 
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.
 
Back
Top