Click here to Skip to main content
15,896,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone,
here i want to read xml file data.Xml file is collection of users where each user has UserName and Modules rights.Here i want selected users modules list using that i can enable and disable asp.menu.

Please help me.
thanks in advance..

What I have tried:

following code is used to read xml file

private void LoadXmlData()
       {
           XmlDocument xmlDoc = new XmlDocument();
           xmlDoc.Load(Server.MapPath("~/App_Data/UserRights.xml"));

           UName = xmlDoc.GetElementsByTagName("LoginName");
           ModulList = xmlDoc.GetElementsByTagName("Modules");

          foreach(XmlNode userNode in  UName)
          {
              if (userNode.InnerText == Session["UserName"].ToString())
               {
                   Response.Write("UserVame="+userNode.InnerText);
                   foreach (XmlNode moduleNode in ModulList)
                   {
                       Modules.Add(moduleNode.InnerText);

                   }

               }
           }

       }



this shows me all users modules list.

this is my xml file
<Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <Rights>
    <Right>
      <LoginName>Kundan.Khadapkar</LoginName>
      <Modules>Inter-Location Transfer</Modules>
    </Right>
    <Right>
      <LoginName>Kundan.Khadapkar</LoginName>
      <Modules>FileDownloder</Modules>
    </Right>

    
    <Right>
      <LoginName>Kundan.Khadapkar</LoginName>
      <Modules>Item Master</Modules>
    </Right>
    <Right>
      <LoginName>Pranaysail</LoginName>
      <Modules>Inter-Location Transfer</Modules>
    </Right>

    <Right>
      <LoginName>Pranaysail</LoginName>
      <Modules>UpLoadDownloadSetup</Modules>
    </Right>
    <Right>
      <LoginName>Abhijit.Adik</LoginName>
      <Modules>Inter-Location Transfer</Modules>
    </Right>
    <Right>
      <LoginName>Abhijit.Adik</LoginName>
      <Modules>FileDownloder</Modules>
    </Right>
  </Rights>
</Root>



Here i want only selected users modules list..
Posted
Updated 3-May-17 1:31am
Comments
SujataJK 3-May-17 6:46am    
Somebody please help me...

1 solution

You want to use appendChild for creating new child in the current file.
I don't see Modules think it is a list or something.
private void LoadXmlData()
       {
           XmlDocument xmlDoc = new XmlDocument();
           xmlDoc.Load(Server.MapPath("~/App_Data/UserRights.xml"));
           var userList = new list(of string, string)
           var nodes = (xmlDoc.GetElementsByTagName("Rights")[0]).GetElementsByTagName("Right");

           foreach (node in nodes)
           {
               var login = node.GetElementsByTagName("UserName")[0];
               var module = node.GetElementsByTagName("Modules")[0];

               // this is what you would like to do instead of write appendChild
               newNode = xmlDoc.createElement("UserVame");
               node.GetElementsByTagName("Modules")[0].appendChild(newNode)

               //you can save it if needed
               userList.add(login, module)
           }
       }
 
Share this answer
 
v3
Comments
SujataJK 4-May-17 0:50am    
thanks @Wessel

but following line shows errors
//i think deceleration error in var userlist
var userList = new list(of string, string)

var nodes = (xmlDoc.GetElementsByTagName("Rights")[0]).GetElementsByTagName("Right");
Wessel Beulink 4-May-17 7:09am    
Duplicated entry?
SujataJK 4-May-17 7:16am    
i can't understand what you are asking.
Wessel Beulink 4-May-17 7:50am    
You need to import generics it's generic.list delete te line if you don't want to save Te values
SujataJK 5-May-17 2:31am    
Now it shows error at
var nodes = (xmlDoc.GetElementsByTagName("Rights")[0]).GetElementsByTagName("Right");

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900