如何在XML文件中存储多个项以方便解析?

时间:2022-08-26 23:18:42

Suppose I have a number of elements to store like:

假设我有一些元素要存储,比如:

fruitList = "apple", "orange", "banana", "kiwi", ...

How would you store these in XML?

如何将它们存储在XML中?

<FruitList>"apple", "orange", "banana", "kiwi"</FruitList>

OR

<Fruits Type="Expensive" List="apple", "orange", "banana", "kiwi"> </Fruits>

Is there a better way?

有更好的办法吗?

Whatever method is chosen, how can I parse the list easily so that the parsing doesn't need to change if the formatting of the items is changed to something like:

无论选择什么方法,如何方便地解析列表,以便当项目的格式更改为以下内容时,解析不需要更改:

<FruitList>
   "apple", 
   "orange",
   "banana",
   "kiwi"
</FruitList>

5 个解决方案

#1


15  

<Fruits>
 <Fruit>Apple</Fruit>
 <Fruit>Orange</Fruit>
</Fruits>

#2


1  

Have you checked out the XmlSerializer class? That may be useful, and it can certainly handle parsing lists it generated. (Not sure if it's exactly what you want, though.)

您检查过XmlSerializer类吗?这可能是有用的,而且它当然可以处理生成的解析列表。(不过,我不确定这是否是你想要的。)

#3


1  

If you're looking to store application settings, then take a look at the types in the System.Configuration Namespace, or look into using Application Settings.

如果要存储应用程序设置,请查看系统中的类型。配置命名空间,或查看使用应用程序设置。

#4


0  

In your case you could just have

在你的情况下,你可以。

List<string> Fruits;

in your class somewhere which would serialize to

在你的课堂上,它会被序列化。

<Fruits>
   <string>Apple</string>
   <string>Orange</string>
</Fruits>

I believe.

我相信。

For something more involved with nested lists of objects, you might want to declare a class Fruits as a List like so:

对于更涉及嵌套对象列表的内容,您可能希望将类结果声明为如下所示的列表:

[Serializable]
public class Fruits: List<Fruit>
{ 
    // empty public constructor to make it serializable
    public Fruits()
    {
    }

    // ...
}

There are cases where you wouldn't be able to put, for example List<Flavors> directly into the Fruit class and have it serialize a list of lists (or was it an array of lists or a list of arrays? I forget---I know I had a problem with this); you would need another class Flavors : List<Flavors> and so forth.

有些情况下,您无法将列表< flavor >直接放入水果类中,并让它序列化列表列表(或者是列表数组或数组列表)?我忘记了,我知道我有个问题;您需要另一个类口味:列表< flavor >等等。

#5


0  

The only reason to use XML in the first place is so that you can use an XML parser to parse its content. This becomes staggeringly clear when you start working with the tools that make XML genuinely useful, like schemas and transforms (or XML serialization): any data structures that you embed in text content are unavailable to those tools.

首先使用XML的唯一原因是,您可以使用XML解析器来解析其内容。当您开始使用使XML真正有用的工具(如模式和转换(或XML序列化))时,这就变得异常清晰:您在文本内容中嵌入的任何数据结构对这些工具都是不可用的。

#1


15  

<Fruits>
 <Fruit>Apple</Fruit>
 <Fruit>Orange</Fruit>
</Fruits>

#2


1  

Have you checked out the XmlSerializer class? That may be useful, and it can certainly handle parsing lists it generated. (Not sure if it's exactly what you want, though.)

您检查过XmlSerializer类吗?这可能是有用的,而且它当然可以处理生成的解析列表。(不过,我不确定这是否是你想要的。)

#3


1  

If you're looking to store application settings, then take a look at the types in the System.Configuration Namespace, or look into using Application Settings.

如果要存储应用程序设置,请查看系统中的类型。配置命名空间,或查看使用应用程序设置。

#4


0  

In your case you could just have

在你的情况下,你可以。

List<string> Fruits;

in your class somewhere which would serialize to

在你的课堂上,它会被序列化。

<Fruits>
   <string>Apple</string>
   <string>Orange</string>
</Fruits>

I believe.

我相信。

For something more involved with nested lists of objects, you might want to declare a class Fruits as a List like so:

对于更涉及嵌套对象列表的内容,您可能希望将类结果声明为如下所示的列表:

[Serializable]
public class Fruits: List<Fruit>
{ 
    // empty public constructor to make it serializable
    public Fruits()
    {
    }

    // ...
}

There are cases where you wouldn't be able to put, for example List<Flavors> directly into the Fruit class and have it serialize a list of lists (or was it an array of lists or a list of arrays? I forget---I know I had a problem with this); you would need another class Flavors : List<Flavors> and so forth.

有些情况下,您无法将列表< flavor >直接放入水果类中,并让它序列化列表列表(或者是列表数组或数组列表)?我忘记了,我知道我有个问题;您需要另一个类口味:列表< flavor >等等。

#5


0  

The only reason to use XML in the first place is so that you can use an XML parser to parse its content. This becomes staggeringly clear when you start working with the tools that make XML genuinely useful, like schemas and transforms (or XML serialization): any data structures that you embed in text content are unavailable to those tools.

首先使用XML的唯一原因是,您可以使用XML解析器来解析其内容。当您开始使用使XML真正有用的工具(如模式和转换(或XML序列化))时,这就变得异常清晰:您在文本内容中嵌入的任何数据结构对这些工具都是不可用的。