[CHARP]
XDocument doc = XDocument.Load("file2path\file2name.xml");
int S_N=(int)doc.Element("SOME_NUMBER").Value;
int S_T1=(int)doc.Element("SOME_TYPE1").Value;
int S_T2=(int)doc.Element("SOME_TYPE2").Value;
int S_R=(int)doc.Element("SOME_REVISION").Value;
int S_C=(int)doc.Element("SOME_CODE").Value;
I see you are trying to read the entire file.. what if the data file is about 2MB... can the C# application handle that much data without crashing?
2mb can hardly be considered a lot of data Memory consumption will be less if you don't read the entire file into memory in one go but instead parse it line by line using streamreader.ReadLine ().
Code:
int S_N = int.Parse (doc.Element("SOME_NUMBER").Value);
You can't cast a string to an int, but you can parse an integer value from a string.
NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.
Another approach is to leverage Xml Serialization which reads an xml file and creates a C# class instance.
The first thing to do is create a class that represents the xml schema. You can use tools to do this like XSD.exe, but on something this simple it's easy enough to add the required serialization attributes.
First I've modified the xml file to follow typical naming conventions and take a few enum types:
Next, create the DBase serialization class which has a static method that reads the xml file and creates an instance of the class. Notice the Serialization attributes which decorate the class and its properties. These attributes let the serializer know what properties to serialize and how.
you did a pretty excellent job by giving that example. Now I understand how to read xml files...
But this is not how my xml file is to be used. The xml file i have, has the tags (which are nothing but the variables) and values within the tags(which are nothing but the number of characters to read from the data file).
so, once again - if my tag says
<SOME_NUMBER>8</SOME_NUMBER>
that means i need to read 8 characters from the data file which contains
0908070605040302
I should fill my textbox with 8 characters 09080706 for the field named SOME_NUMBER and so on...
you did a pretty excellent job by giving that example. Now I understand how to read xml files...
But this is not how my xml file is to be used. The xml file i have, has the tags (which are nothing but the variables) and values within the tags(which are nothing but the number of characters to read from the data file).
So I gave you an example of how to read a string, integer and enum values from xml. Modify my example as appropriate for your needs. My goal isn't to do the work for you, but to give you a technique that you can use to solve your problem.
I thought I would have to declare enums for reading numbers too. I guess I missed that part of the code and realized it later but it was too late and I had already posted again... but yes, you're right... now I can use this snippet to serve my purpose..
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.