如何从 XML 文件导入/读取数据?

问题描述:

如何在 C# 中访问 XML 文件?如何计算该xml文件中的节点数?我应该如何访问该 xml 文件中的每个节点?

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?

我有两个 xml 文件,其中一个是 dev.xml,其中包含此代码

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>

第二个文件是guj.xml(结构非常相似)

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.

由于您添加了更多详细信息,我现在可以提供更好的答案.这是一个功能性的 xml 解析和加入控制台应用程序,它演示了您正在寻找的内容(我认为).要解析 xml 文件而不是 xml 字符串,请使用 XDocument Load 方法而不是显示的 Parse 方法.祝你好运,

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

每个会话变量请求:

匿名类型仅用于方法中.要将列表放入 Session 变量以在其他地方使用,请创建您自己的真实类,其中包含 3 个所需的属性,并将上面的代码行更改为与下面的非常匹配.(我选择的类名是CrossReferenceTranslation.)

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>();