How to access an XML file in C#? How to count the number of nodes in that xml file? How am i supposed to access each and every node in that xml file?
I have two xml files, one of them is dev.xml which has this code
<Devanagri_to_itrans>
<mapping>
<character>अ</character>
<itrans>a</itrans>
</mapping>
...
</Devanagri_to_itrans>
the second file is guj.xml (with a very similar structure)
<Gujrathi_to_itrans>
<mapping>
<character>અ</character>
<itrans>a</itrans>
<mapping>
...
</Gujrathi_to_itrans>
I need to turn this into a two-dimension arraying of the character mappings.
Try this tutorial on Linq to XML - http://www.switchonthecode.com/tutorials/introduction-to-linq-simple-xml-parsing
This question - How to iterate through an XDocument, getting complete XML structure, object by object? - also provides some interesting code
Since you've added more details I can now provide a better answer. Here is a functional xml parsing and joining console app that demonstrates what it is you're looking for (I think). To parse xml files rather than xml strings use the
XDocument
Load method rather than the displayed Parse method. Good luck,PER REQUEST FOR SESSION VARIABLE:
Anonymous types are only intended for use within a method. To place a list into a Session variable for use elsewhere create a real class of your own that contains the 3 desired properties and change the line of code above very matching this to the below. (The class name I chose was
CrossReferenceTranslation
.)...then, at some other point in time you can do this to get your session object list into a variable. Note the assumption that the variable could be null, which would happen whenever a session has timed out...
If you're using .net 3.5 or later use LINQ to XML by setting a reference to
System.Xml.Linq
. Here's a simple count of elements in a given xml file to a console app's window: