本文介绍了在 C# 中遍历 XML 树的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 .net 和 c# 的新手,所以我想确保我使用的是正确的工具来完成这项工作.

I'm new to .net and c#, so I want to make sure i'm using the right tool for the job.

我收到的 XML 是对另一台机器上的目录树的描述,因此它有很多层次.我现在需要做的是获取 XML 并创建对象结构(自定义类),并使用来自 XML 输入的信息填充它们,例如文件、文件夹、标签、属性...

The XML i'm receiving is a description of a directory tree on another machine, so it go many levels deep. What I need to do now is to take the XML and create a structure of objects (custom classes) and populate them with info from the XML input, like File, Folder, Tags, Property...

在我看来,这个 XML 输入的树结构使其成为使用递归遍历树的主要候选者.

The Tree stucture of this XML input makes it, in my mind, a prime candidate for using recursion to walk the tree.

在 .net 3.5 中是否有不同的方法来做到这一点?

Is there a different way of doing this in .net 3.5?

我看过 XmlReaders,但它们似乎以线性方式在树上行走,并不是我真正想要的......

I've looked at XmlReaders, but they seem to be walking the tree in a linear fashion, not really what i'm looking for...

我收到的 XML 是第 3 方 api 的一部分,因此不在我的控制范围内,并且可能会在未来发生变化.

The XML i'm receiving is part of a 3rd party api, so is outside my control, and may change in the futures.

我已经研究了反序列化,但它的缺点(黑盒实现,需要将成员声明为公共,速度慢,仅适用于简单对象......)也将其从列表中删除.

I've looked into Deserialization, but it's shortcomings (black box implementation, need to declare members a public, slow, only works for simple objects...) takes it out of the list as well.

感谢您对此的意见.

推荐答案

我将使用 System.Xml.Linq 中的 XLINQ 类(这是命名空间和您需要引用的程序集).将 XML 加载到 XDocument 中:

I would use the XLINQ classes in System.Xml.Linq (this is the namespace and the assembly you will need to reference). Load the XML into and XDocument:

XDocument doc = XDocument.Parse(someString);

接下来您可以使用递归或伪递归循环来迭代子节点.您可以选择您的子节点,例如:

Next you can either use recursion or a pseudo-recursion loop to iterate over the child nodes. You can choose you child nodes like:

//if Directory is tag name of Directory XML
//Note: Root is just the root XElement of the document
var directoryElements = doc.Root.Elements("Directory");

//you get the idea
var fileElements = doc.Root.Elements("File");

变量 directoryElementsfileElements 将是 IEnumerable 类型,这意味着您可以使用类似 foreach 的东西来遍历所有元素.构建元素的一种方法是这样的:

The variables directoryElements and fileElements will be IEnumerable types, which means you can use something like a foreach to loop through all of the elements. One way to build up you elements would be something like this:

List<MyFileType> files = new List<MyFileType>();

foreach(XElelement fileElement in fileElements)
{
  files.Add(new MyFileType()
    {
      Prop1 = fileElement.Element("Prop1"), //assumes properties are elements
      Prop2 = fileElement.Element("Prop2"),
    });
}

在示例中,MyFileType 是您创建的用于表示文件的类型.这有点暴力攻击,但它会完成工作.

In the example, MyFileType is a type you created to represent files. This is a bit of a brute-force attack, but it will get the job done.

如果你想使用 XPath,你需要使用 System.Xml.XPath.

If you want to use XPath you will need to using System.Xml.XPath.

关于 System.Xml 与 System.Xml.Linq 的说明

自 1.0 天以来,.Net 中有许多 XML 类.这些(大部分)存在于 System.Xml 中.在 .Net 3.5 中,在 System.Xml.Linq 下发布了一组精彩的新 XML 类.我不能过分强调它们比 System.Xml 中的旧类好用多少.我会向任何 .Net 程序员,尤其是刚开始使用 .Net/C# 的人强烈推荐它们.

There are a number of XML classes that have been in .Net since the 1.0 days. These live (mostly) in System.Xml. In .Net 3.5, a wonderful, new set of XML classes were released under System.Xml.Linq. I cannot over-emphasize how much nicer they are to work with than the old classes in System.Xml. I would highly recommend them to any .Net programmer and especially someone just getting into .Net/C#.

这篇关于在 C# 中遍历 XML 树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 12:43