Newbie to XML

Discussion in 'ASP .Net' started by Sorin Sandu, Jul 30, 2004.

  1. Sorin Sandu

    Sorin Sandu Guest

    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 Sandu, Jul 30, 2004
    #1
    1. Advertisements

  2. Sorin Sandu

    Hans Kesting Guest

    I've never used XmlTextReader before, so I don't know what the problem
    is with this particular code. There are however easier methods to search
    through an xml file, using XmlDocument and XPath:

    XmlDocument xml = new XmlDocument();
    xml.Load("f:\\intranet\\users.xml");

    XmlNode userNode = xml.SelectSingleNode("/users/name[text() = 'ASPNET']");
    if (userNode != null)
    {
    Label1.Text = userNode.Attributes["level"].Value;
    }



    Hans Kesting
     
    Hans Kesting, Jul 30, 2004
    #2
    1. Advertisements

  3. Sorin Sandu

    bruce barker Guest

    the XmlReader reads a node at a time. in the xml

    <name level='0' >ASPNET</name>

    there are two nodes, a start element node and a text node. the node <name
    level='0'> has no string value.

    if (reader.NodeType == XmlNodeType.Element)
    {
    if (reader.Name == "name")
    {
    if (!reader.IsEmptyElement)
    {
    string mylevel = reader.GetAttribute("level"); // need read
    before getting text node
    reader.Read();
    string test = reader.ReadString();
    if (test.ToUpper() == suser.ToUpper())
    Label1.Text = mylevel;
    }
    }
     
    bruce barker, Jul 30, 2004
    #3
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.