How to import/read data from an XML file?

2019-05-28 19:27发布

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.

3条回答
再贱就再见
3楼-- · 2019-05-28 20:06

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,

            XDocument docA = XDocument.Parse(
@"<Devanagri_to_itrans>
  <mapping>
    <character>अ</character>
    <itrans>a</itrans>
  </mapping>
</Devanagri_to_itrans>");
            XDocument docB = XDocument.Parse(
@"<Gujrathi_to_itrans>
  <mapping>
     <character>અ</character>
     <itrans>a</itrans>
  </mapping>
</Gujrathi_to_itrans>");
            var devanagriKeys = (from d in docA.Descendants("mapping")
                                                  select new {
                                                      Key = d.Descendants("itrans").FirstOrDefault().Value,
                                                      Character = d.Descendants("character").FirstOrDefault().Value
                                                  }).ToArray();
            var gujrathiKeys = (from g in docB.Descendants("mapping")
                                                  select new {
                                                      Key = g.Descendants("itrans").FirstOrDefault().Value,
                                                      Character = g.Descendants("character").FirstOrDefault().Value
                                                  }).ToArray();
            var crossReference = (from d in devanagriKeys
                                  join g in gujrathiKeys on d.Key equals g.Key
                                  select new {
                                        d.Key,
                                        Devanagri = d.Character,
                                        Gujrathi = g.Character
                                    }).ToList();
            Console.WriteLine("Enter a key character to translate:");
            string searchKey = Console.ReadLine();
            var translation = crossReference.Where(cr => cr.Key == searchKey).FirstOrDefault();
            if (translation == null) 
                Console.WriteLine("No such key in the cross reference.");
            else
                Console.WriteLine("{0} is {1} in Devanagri and {2} in Gujrathi", 
                    translation.Key, translation.Devanagri, translation.Gujrathi);
            Console.ReadKey(true);

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.)

        Session["CrossReference"] = (from d in devanagriKeys
                              join g in gujrathiKeys on d.Key equals g.Key
                              select new CrossReferenceTranslation() {
                                    d.Key,
                                    Devanagri = d.Character,
                                    Gujrathi = g.Character
                                }).ToList();

...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...

List<CrossReferenceTranslation>() crossReference = Session["CrossReference"] ?? 
   new List<CrossReferenceTranslation>();
查看更多
冷血范
4楼-- · 2019-05-28 20:09

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:

string xml = @"<xml><a/><a/><a/></xml>";
XDocument doc = XDocument.Parse(xml);
Console.WriteLine((from a in doc.Descendants("a")
                   select a).Count());
查看更多
登录 后发表回答