Click here to Skip to main content
Sign Up to vote bad
good
See more: C#XMLXmlReader
Hello,
 
I have and xml file, like this:
 
<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:
 
            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 5 Nov '12 - 0:41

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

1 solution

Try using XmlDocument.
 
It goes something like this
 
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
  Permalink  

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Arun Vasu 339
1 OriginalGriff 320
2 Sergey Alexandrovich Kryukov 315
3 Tadit Dash 221
4 CPallini 178
0 Sergey Alexandrovich Kryukov 9,955
1 OriginalGriff 7,589
2 CPallini 4,028
3 Rohan Leuva 3,422
4 Maciej Los 2,949


Advertise | Privacy | Mobile
Web04 | 2.6.130523.1 | Last Updated 5 Nov 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid