Click here to Skip to main content
15,943,335 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hi!!i want to read a xml file and also store the read value in string variable in asp.net
Posted

Let me start with the end: "store… value in string variable" is something done by assigning a value to a variable. If you did not understand it, I am not sure if it make any sense to talk about XML or not. :-)

Anyway, there are at least three ways to read XML supported by standard .NET libraries. Here is my short review of them:

  1. Use System.Xml.XmlDocument class. It implements DOM interface; this way is the easiest and good enough if the size if the document is not too big.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx[^].
  2. Use the class System.Xml.XmlTextReader; this is the fastest way of reading, especially is you need to skip some data.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.aspx[^].
  3. Use the class System.Xml.Linq.XDocument; this is the most adequate way similar to that of XmlDocument, supporting LINQ to XML Programming.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx[^], http://msdn.microsoft.com/en-us/library/bb387063.aspx[^].


—SA
 
Share this answer
 
Comments
VJ Reddy 10-Apr-12 23:36pm    
Good answer. +5
Sergey Alexandrovich Kryukov 11-Apr-12 10:42am    
Thank you, VJ.
--SA
C#
string value1;
string value2;

protected void Page_Load(object sender, EventArgs e)
{
   Read_Xmlfile();
}

public void Read_Xmlfile()
{
   XmlTextReader reader = new XmlTextReader("D:\\WebSite2008\\WebSite3Tierdemo2\\file\\myxml.xml");
   while (reader.Read())
   {
      XmlNodeType nodeType = reader.NodeType;
      switch (nodeType)
      {
         case XmlNodeType.Element:
            if (reader.HasAttributes)
            {
               for (int i = 0; i < reader.AttributeCount; i++)
               {
                  reader.MoveToAttribute(i);
                  //value2=reader.Value;
                  //Response.Write("Attribute is {0} with Value {1}: ", reader.Name, reader.Value);
               }
            }
            break;
         
         case XmlNodeType.Text:
            //Response.Write(reader.Value);
            value1 = reader.Value;
            Response.Write(value1);
            break;
      }
   }
   //Label1.Text = value1;
   //Label2.Text = value2;
}
 
Share this answer
 
v4
Also you may do the following :

C#
String path = Server.MapPath("~\\App_Data\\Ooohh.xml");
        if (File.Exists(path))
        {
            DataSet ds = new DataSet();
            ds.ReadXml(path);
            GridView1.DataSource = ds;
            GridView1.DataBind();
            ds.Dispose();
        }


In this case the XML is read as a table and shown in the GridView1. Not sure what you mean by "store the read value in string variable".
 
Share this answer
 

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