[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 613
  • Last Modified:

asp.net 1.1 c# read xml file to determine if data is in file

How do I query an xml file to determine if an ip address is in the file and redirect the user if the value is found?

private void Page_Load(object sender, System.EventArgs e)
{
  //check to see if the user's ip address is currently in the xml file, if so then redirect
  if (File.Exists(Server.MapPath("votes.xml"))==true )
   {
     string strIP = Request.ServerVariables["REMOTE_ADDR"].ToString();
     ds=new DataSet();
     ds.ReadXml( Server.MapPath("votes.xml"));
     //how do I query the file contents in the dataset?
     if ((strIP) == true)
       {
          server.transfer("error.aspx");
       }
   }
 else
  {
    //do nothing
  }
}

contents of xml file:
<?xml version="1.0" standalone="yes"?>
<root>
  <vote>
    <val>1</val>
    <ip>127.0.0.1</ip>
  </vote>
  <vote>
    <val>2</val>
    <ip>127.0.0.1</ip>
  </vote>
</root>




0
fwsteal
Asked:
fwsteal
2 Solutions
 
Sammy AgeilCommented:
read this, short and sweet tutorial
http://www.developerfusion.co.uk/show/3077/

Good luck
0
 
william007Commented:
Try this..
private void Page_Load(object sender, System.EventArgs e)
            {
                  
                  
                  if (File.Exists(Server.MapPath("votes.xml"))==true )
                  {
                        string strIP = Request.ServerVariables["REMOTE_ADDR"].ToString();
                    ds=new DataSet();
                        ds.ReadXml(Server.MapPath("votes.xml"));
                        int iTableCount,iRowCount;
                        bool bIPExist=false;
                        iTableCount=ds.Tables.Count;
                        for(int i=0;i<iTableCount;i++)
                        {
                              iRowCount = ds.Tables[i].Rows.Count;
                              for(int j=0;j<iRowCount;j++)
                              {
                                    if ((String)ds.Tables[i].Rows[j].ItemArray[1] == strIP)
                                    {
                                          bIPExist = true;
                                          break;

                                    }
                              }
                              if (bIPExist ==true)
                              {
                                    break;
                              }
                              
                        }
                  }

                  //using bIPExist to decide whether go to errorpage or do nothing
                   if (bIPExist == true){
                                  server.transfer("error.aspx");
      }
                  else
                  {
                        //do nothing
                  }
            }
0
 
the_paabCommented:
Solution without using of dataset.

   if (File.Exists(Server.MapPath("votes.xml"))==true )
   {
     string strIP = Request.ServerVariables["REMOTE_ADDR"].ToString();
     StreamReader sr = new StreamReader(Server.MapPath("votes.xml"));
     String str = sr.ReadToEnd();
     XmlDocument doc = new XmlDocument();
     doc.LoadXml(str);
     XmlNode root = doc.DocumentElement;
     XmlNode vote;
 
     for (int i=0; i<root.ChildNodes.Count; i++)
      {
          vote = root.ChildNodes[i];
          if (vote["ip"].InnerText != "")
          {
            //server.transfer("error.aspx");
          }          
       }        
    }
 else
  {
    //do nothing
  }
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
RejojohnyCommented:
ds=new DataSet();
ds.ReadXml(Server.MapPath("votes.xml"));

//find the ip using rowfilter
ds.Tables[0].DefaultView.RowFilter="ip='" + strIP + "'" ;
DataView ldsvFind = ds.Tables[0].DefaultView;
if (ds.Count >0)
{
          server.transfer("error.aspx");;
}

there are many other ways to search a dataset .. u can use dataset.select or dataset.find etc etc .. so there is no need to loop through each record .. using find is the fastest as u can define the ip address column as the primary key .. so if u assume this file to be really huge, u could look at that option ..

have a look here for examples
http://www.akadia.com/services/dotnet_find_methods.html

Rejo
0
 
fwstealAuthor Commented:
william007

error CS0103: The name 'bIPExist' does not exist in the class or namespace 'polls.WebForm1'
if (bIPExist ==true)
0
 
fwstealAuthor Commented:
rejojohny

cs(69): 'System.Data.DataSet' does not contain a definition for 'Count'
if (ds.Count >0)
0
 
william007Commented:
Hi, sorry for that, to correct the problem,  move the bool bIPExist to the correct scope.

Followed is the correct code
...  
               bool bIPExist=false;//<--Move it here
                  if (File.Exists(Server.MapPath("votes.xml"))==true )
                  {
                        string strIP = Request.ServerVariables["REMOTE_ADDR"].ToString();
                        ds=new DataSet();
                        ds.ReadXml(Server.MapPath("votes.xml"));
                        int iTableCount,iRowCount;
                        
                        iTableCount=ds.Tables.Count;
                        for(int i=0;i<iTableCount;i++)
...
0
 
william007Commented:
>>cs(69): 'System.Data.DataSet' does not contain a definition for 'Count'
>>if (ds.Count >0)

for this problem, you need to change to
ldsvFind.Count
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now