如何读取结构未知的XML

时间:2021-10-21 14:31:14

I'm coding an XML viewer for a school assignment but I don't have any clue how I can load XML-files without knowing the structure of the file.
I'll display the XML structure in a listbox after I've read the file.

我正在为一个学校作业编写XML查看器,但我不知道如何在不了解文件结构的情况下加载XML文件。在读取文件之后,我将在列表框中显示XML结构。

It's a school assignment and This should be the result : 如何读取结构未知的XML

这是学校的作业,这应该是结果:

I have an example file that I can load perfectly but I'm stuck with a random file.

我有一个示例文件,我可以很好地加载,但是我被一个随机文件卡住了。

XmlDocument doc = new XmlDocument();
using(XmlReader xmlReader = XmlReader.Create("c:\\temp\\sites.xml"))
{          
   //Load file
   doc.Load(xmlReader);
   XmlNode root = doc.DocumentElement;

   foreach (XmlNode siteNode in root)
   {
      //Nodes
      XmlNode URLNode = siteNode.FirstChild;
      XmlNode EmailNode = siteNode.LastChild;

      //Create site
      Site site = new Site(URLNode.FirstChild.Value, EmailNode.FirstChild.Value);

      //Add to list
      sites.Add(site);
  }
  Console.WriteLine(sites.Count);
}

4 个解决方案

#1


8  

You're not going to be able to turn the XML data into known classes (like your Site) class without knowing something about the structure, as you'd need some way to migrate the data to the class constructor parameters/properties/etc.

您将无法将XML数据转换为已知的类(比如您的站点)类,而不需要了解结构,因为您需要某种方式将数据迁移到类构造函数参数/属性/等。

That being said, if you just want to view or inspect the XML file itself, you can just recursively inspect the XmlNode elements inside the root element.

也就是说,如果您只想查看或检查XML文件本身,那么只需递归地检查根元素中的XmlNode元素。

XmlNode includes all of the properties you'd need to do this, such as ChildNodes, Attributes, and Value.

XmlNode包含了实现此目的所需的所有属性,如子节点、属性和值。

#2


0  

Not sure about the use case but while writing a small job scheduler, I represented each job as a set of 3 things - a scheduler, a fully qualified class name implementing a particular job, some textual job context (XML). When a scheduler needed to execute a job at scheduled time, while it was creating a job class using reflection and known location of the assemblies, it passed XML context to a job constructor, which knew how to deal with it.

不确定用例,但是在编写一个小作业调度器时,我将每个作业表示为一组三样东西——调度器、实现特定作业的完全限定类名、一些文本作业上下文(XML)。当调度程序需要在预定时间执行作业时,当它使用映像和程序集的已知位置创建作业类时,它将XML上下文传递给一个作业构造函数,该构造函数知道如何处理它。

Similarly if it's possible for you to define a class within the XML, then you can read it first and act accordingly.

类似地,如果您可以在XML中定义一个类,那么您可以先阅读它并相应地采取行动。

#3


0  

If it's only a viewer, why not just load the XML into a DataSet or DataTable and then bind it to a DataGrid

如果只是查看器,为什么不直接将XML加载到数据集或DataTable中,然后将其绑定到DataGrid中呢

#4


0  

If I were you, I'd try using LINQ to XML, with emphasis on the functionality provided by XElement - it's friendlier and easier imo. I also recommend that you use recursion (as Reed Copsey suggested) to traverse your XML.

如果我是您,我将尝试使用LINQ到XML,重点关注XElement提供的功能——它更友好、更简单。我还建议您使用递归(正如Reed Copsey建议的那样)遍历XML。

#1


8  

You're not going to be able to turn the XML data into known classes (like your Site) class without knowing something about the structure, as you'd need some way to migrate the data to the class constructor parameters/properties/etc.

您将无法将XML数据转换为已知的类(比如您的站点)类,而不需要了解结构,因为您需要某种方式将数据迁移到类构造函数参数/属性/等。

That being said, if you just want to view or inspect the XML file itself, you can just recursively inspect the XmlNode elements inside the root element.

也就是说,如果您只想查看或检查XML文件本身,那么只需递归地检查根元素中的XmlNode元素。

XmlNode includes all of the properties you'd need to do this, such as ChildNodes, Attributes, and Value.

XmlNode包含了实现此目的所需的所有属性,如子节点、属性和值。

#2


0  

Not sure about the use case but while writing a small job scheduler, I represented each job as a set of 3 things - a scheduler, a fully qualified class name implementing a particular job, some textual job context (XML). When a scheduler needed to execute a job at scheduled time, while it was creating a job class using reflection and known location of the assemblies, it passed XML context to a job constructor, which knew how to deal with it.

不确定用例,但是在编写一个小作业调度器时,我将每个作业表示为一组三样东西——调度器、实现特定作业的完全限定类名、一些文本作业上下文(XML)。当调度程序需要在预定时间执行作业时,当它使用映像和程序集的已知位置创建作业类时,它将XML上下文传递给一个作业构造函数,该构造函数知道如何处理它。

Similarly if it's possible for you to define a class within the XML, then you can read it first and act accordingly.

类似地,如果您可以在XML中定义一个类,那么您可以先阅读它并相应地采取行动。

#3


0  

If it's only a viewer, why not just load the XML into a DataSet or DataTable and then bind it to a DataGrid

如果只是查看器,为什么不直接将XML加载到数据集或DataTable中,然后将其绑定到DataGrid中呢

#4


0  

If I were you, I'd try using LINQ to XML, with emphasis on the functionality provided by XElement - it's friendlier and easier imo. I also recommend that you use recursion (as Reed Copsey suggested) to traverse your XML.

如果我是您,我将尝试使用LINQ到XML,重点关注XElement提供的功能——它更友好、更简单。我还建议您使用递归(正如Reed Copsey建议的那样)遍历XML。