Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I have and xml file, like this:

XML
<MyXMLFile>
  <SerialNumber>111</SerialNumber>
  <Name>MyName</Name>
  <Size X="10" Y="12" />
  </MyXMLFile>


The user who creates the XML data file, can change the order of the elements.
For example, "size" element can be before "name" element.

I have to read the whole data of this file.

As far as I know, XMLReader can't go back from it's position.
So one solution I thought about, is to create an xml reader and destroy it, again and again.
For example:

C#
XmlReader xmlReader = XmlReader.Create(strXMLFileName);

xmlReader.ReadToFollowing(XMLNODE_Size);
int sizeX = Convert.ToInt32(xmlReader.GetAttribute("X"));
int sizeY= Convert.ToInt32(xmlReader.GetAttribute("Y"));
xmlReader.Close();

xmlReader = XmlReader.Create(strXMLFileName);
xmlReader.ReadToFollowing(XMLNODE_NAME);
xmlReader.ReadStartElement();
string name = xmlReader.ReadString();
xmlReader.ReadEndElement();
xmlReader.Close();


Is there a better way to do it, instead of close and reopen the xmlReader?
(I want also to read the attibutes where there are.)

Thanks
Posted
Comments
Storxstar 5-Nov-12 6:45am    
Not sure if this will help but try using a while loop. While(XmlReader.Read())

Try using XmlDocument.

It goes something like this

C#
XmlDocument xDoc = new XmlDocument();
xDoc.Load("YourFile.Xml");

string sizeValue = xDoc.DocumentElement.SelectSingleNode("Size").InnerText;
string nameValue = xDoc.DocumentElement.SelectSingleNode("Name").InnerText;
string serialNumbersizeValue = xDoc.DocumentElement.SelectSingleNode("SerialNumber").InnerText;


Hope that helps. If it does mark the answer as solution/upvote.

Thanks
Milind
 
Share this answer
 
v2
//Method to read XML File and feel out
dropdownlist


C#
public void GetAllLocations()
       {
           DataSet ds = new DataSet();
           ds.ReadXml(Server.MapPath("~\\App_Data\\Location.xml"));

           try
           {
               if (ds.Tables[0].Rows.Count > 0)
               {
                   DDLLocation.Enabled = true;
                   DDLLocation.DataSource = ds;
                   DDLLocation.DataTextField = "LocationName";
                   DDLLocation.DataValueField = "LocationName";
                   DDLLocation.DataBind();
                   DDLLocation.Items.Insert(0, new ListItem("(Please Select)", Convert.ToString(0)));   // Add as first item

               }
               else
               {
                   DDLLocation.Items.Insert(0, new ListItem("Location not found", Convert.ToString(0)));
                   return;
               }

           }
           catch (Exception ex)
           {
               lblMessage.Text = "Load Location... " + ex.Message.ToString();
               return;
           }

       }
 
Share this answer
 
//XML DATA
XML
<mydata>
  <admin>
    <username>
      admin
    </username>
    <password>
      admin1111
    </password>
  </admin>


  <admin>
    <username>
      admin1234
    </username>
    <password>
      1234
    </password>
  </admin>
</mydata>



//Method to read XML File
C#
XmlDocument xDoc = new XmlDocument();
          xDoc.Load("login.xml"); //XML File name (if winforms keep the XML file in bin directory)

         //Case 1: When only one value in xml File

          string username = xDoc.SelectSingleNode("mydata/admin/username").InnerText.Trim();
          string password = xDoc.SelectSingleNode("mydata/admin/password").InnerText.Trim();

         //Case 1 Ends

          //Case 2: when more than one value in xml File (looping)

          var mydata = xDoc.SelectNodes("mydata");
          for (int i = 0; i < mydata.Count; i++)
          {
              foreach (XmlNode node in mydata[i].ChildNodes)
              {
                  string uname = node.SelectSingleNode("username").InnerText.Trim();
                  string pwd = node.SelectSingleNode("password").InnerText.Trim();
              }
          }


//Case 2 Ends
 
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