如何从 XML 文件导入/读取数据?
如何在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试有关 Linq to XML 的教程 - http://www. switchonthecode.com/tutorials/introduction-to-linq-simple-xml-parsing
这个问题 - 如何迭代 XDocument,逐个对象获取完整的 XML 结构? - 还提供了一些有趣的代码
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
如果您使用的是 .net 3.5 或更高版本,请通过设置对
System.Xml.Linq
的引用来使用 LINQ to XML。以下是控制台应用程序窗口的给定 xml 文件中元素的简单计数:
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:
既然您添加了更多详细信息,我现在可以提供更好的答案。
这是一个功能性的 xml 解析和连接控制台应用程序,它演示了您正在寻找的内容(我认为)。要解析 xml 文件而不是 xml 字符串,请使用
XDocument
Load 方法而不是显示的 Parse 方法。祝你好运,每个会话变量请求:
匿名类型仅适用于方法内。要将列表放入 Session 变量中以便在其他地方使用,请创建一个您自己的真实类,其中包含 3 个所需的属性,并将上面的代码行更改为与下面的非常匹配。 (我选择的类名是
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,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...