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

发布于 2024-10-24 07:59:21 字数 575 浏览 1 评论 0原文

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

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

<Devanagri_to_itrans>
  <mapping>
    <character>अ</character>
    <itrans>a</itrans>
  </mapping>
  ...
</Devanagri_to_itrans>

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

<Gujrathi_to_itrans>
  <mapping>
     <character>અ</character>
     <itrans>a</itrans>
  <mapping>
  ...
</Gujrathi_to_itrans>

我需要将其转换为字符映射的二维数组。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

山人契 2024-10-31 07:59:21

如果您使用的是 .net 3.5 或更高版本,请通过设置对 System.Xml.Linq 的引用来使用 LINQ to XML。
以下是控制台应用程序窗口的给定 xml 文件中元素的简单计数:

string xml = @"<xml><a/><a/><a/></xml>";
XDocument doc = XDocument.Parse(xml);
Console.WriteLine((from a in doc.Descendants("a")
                   select a).Count());

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());
忆悲凉 2024-10-31 07:59:21

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

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

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

...然后,在其他某个时间点,您可以执行此操作,将会话对象列表放入变量中。请注意变量可能为空的假设,只要会话超时就会发生这种情况......

List<CrossReferenceTranslation>() crossReference = Session["CrossReference"] ?? 
   new List<CrossReferenceTranslation>();

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>();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文