如何从xml中读取满足或满足特定条件的数据

时间:2023-01-16 15:48:31

If I have this data model class:

如果我有这个数据模型类:

public class Person
{
    private int _id;
    private int _deptID;
    private string _firstName;
    private string _lastName;

    public int ID
    {
        get { return _deptID; }
        set { _deptID = value; }
    }

    public int DeptID
    {
        get { return _id; }
        set { _id = value; }
    }

    public string FirstName
    {
        get { return _firstName; }
        set { _firstName = value; }
    }

    public string LastName
    {
        get { return _lastName; }
        set { _lastName = value; }
    }

... and then create my data set

...然后创建我的数据集

    private List<Person> GeneratePersonData()
    {
        List<Person> personData = new List<Person>();
      personData.Add(new Person() { ID = 1, DeptID = 25, FirstName = "John", LastName = "Doe" });
        personData.Add(new Person() { ID = 2, DeptID = 25, FirstName = "John", LastName = "Doe" });
        personData.Add(new Person() { ID = 3, DeptID = 105, FirstName = "John", LastName = "Doe" });
        personData.Add(new Person() { ID = 4, DeptID = 43, FirstName = "John", LastName = "Doe" });
        personData.Add(new Person() { ID = 5, DeptID = 25, FirstName = "John", LastName = "Doe" });
        personData.Add(new Person() { ID = 6, DeptID = 32, FirstName = "John", LastName = "Doe" });
        personData.Add(new Person() { ID = 7, DeptID = 2, FirstName = "John", LastName = "Doe" });
        personData.Add(new Person() { ID = 8, DeptID = 25, FirstName = "John", LastName = "Doe" });
        personData.Add(new Person() { ID = 9, DeptID = 2, FirstName = "John", LastName = "Doe" });

        return personData;
    }

And then I store my data into xml like this:

然后我将我的数据存储到xml中,如下所示:

    private void StorePersonData()
    { 
    XmlWriterSettings xmlWriterSettings = new XmlWriterSettings();
        xmlWriterSettings.Indent = true;

        using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
        {
            using (IsolatedStorageFileStream stream = myIsolatedStorage.OpenFile("Person.xml", FileMode.Create))
            {
                XmlSerializer serializer = new XmlSerializer(typeof(List<Person>));
                using (XmlWriter xmlWriter = XmlWriter.Create(stream, xmlWriterSettings))
                {
                    serializer.Serialize(xmlWriter, GeneratePersonData());
                }
            }
        }
    }

Now, what I want to do is read only the persons with a particular deptID from the xml into a new list so I do this:

现在,我想要做的是只将具有特定deptID的人从xml读入一个新列表,所以我这样做:

    private void ReadPersonData(int deptID)
    {
        List<Person>data = new List<Person>();
        List<Person> newData = new List<Person>();

        using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
        {
            using (IsolatedStorageFileStream stream = myIsolatedStorage.OpenFile("Person.xml", FileMode.Open))
            {
                XmlSerializer serializer = new XmlSerializer(typeof(List<Person>));
                 data = (List<Person>)serializer.Deserialize(stream);
            }
        }

        for (int i = 0; i < data.Count; i++)
        {
            if (data[i]._deptID == deptID)
            {
                newData.Add(data[i]);
            }
        }
    }
}

But this method is ridiculously slow if I have a really large data set. Is there a way in xml to read only data that satisfy a certain condition?

但是如果我有一个非常大的数据集,这种方法会非常慢。在xml中是否有方法只读取满足某个条件的数据?

2 个解决方案

#1


0  

You might want to reconsider using XML to store your data. There isn't much structure in your data, and you're doing random access. It really seems like a database would do a better job of storing, indexing, and retrieving/filtering this data; then you can export the result sets to XML as needed.

您可能希望重新考虑使用XML来存储数据。您的数据中没有太多结构,您正在进行随机访问。看起来数据库似乎可以更好地存储,索引和检索/过滤这些数据;然后,您可以根据需要将结果集导出到XML。

#2


0  

No - once you had taken an approach of saving data in XML and then querying it, it has to be slow with large data set.

不 - 一旦你采用了在XML中保存数据然后查询它的方法,它就必须在大数据集的情况下变慢。

is this static data or it changes ? If its a static data, why not keep the xml in memory ?

这个静态数据还是会改变?如果是静态数据,为什么不将xml保留在内存中?

Note 1: Instead of XML, why not serialize in JSON object. the size would be much smaller and hence you would see some improvement.

注1:为什么不在JSON对象中序列化,而不是XML。尺寸会小得多,因此你会看到一些改进。

Note 2 : Saving Data in JSON and XML is good when you are talking with different application(say one built in .net and one built in java). With these open formats you can easily pass data. What I see is, its you storing the data and its you reading the data ? Why XML (or JSON) ??

注意2:当您使用不同的应用程序(比如一个内置的.net和一个内置的java)时,以JSON和XML保存数据是很好的。使用这些开放格式,您可以轻松传递数据。我看到的是,你存储数据和你读数据?为什么选择XML(或JSON)?

UPDATE

For JSON serialization - you could use http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.aspx

对于JSON序列化 - 您可以使用http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.aspx

if this xml never changes then do not read it every time. Read and parse the xml, keep it in memomry, what every way you like (session, static variable, cache).

如果这个xml永远不会改变,那么每次都不要读它。读取并解析xml,将其保存在memomry中,各种方式(会话,静态变量,缓存)。

Why are you not using DB for storing the same information ? One application can write the data in DB and other application would retrieve the data from DB ?

为什么不使用DB存储相同的信息?一个应用程序可以在DB中写入数据,而其他应用程序可以从DB中检索数据?

#1


0  

You might want to reconsider using XML to store your data. There isn't much structure in your data, and you're doing random access. It really seems like a database would do a better job of storing, indexing, and retrieving/filtering this data; then you can export the result sets to XML as needed.

您可能希望重新考虑使用XML来存储数据。您的数据中没有太多结构,您正在进行随机访问。看起来数据库似乎可以更好地存储,索引和检索/过滤这些数据;然后,您可以根据需要将结果集导出到XML。

#2


0  

No - once you had taken an approach of saving data in XML and then querying it, it has to be slow with large data set.

不 - 一旦你采用了在XML中保存数据然后查询它的方法,它就必须在大数据集的情况下变慢。

is this static data or it changes ? If its a static data, why not keep the xml in memory ?

这个静态数据还是会改变?如果是静态数据,为什么不将xml保留在内存中?

Note 1: Instead of XML, why not serialize in JSON object. the size would be much smaller and hence you would see some improvement.

注1:为什么不在JSON对象中序列化,而不是XML。尺寸会小得多,因此你会看到一些改进。

Note 2 : Saving Data in JSON and XML is good when you are talking with different application(say one built in .net and one built in java). With these open formats you can easily pass data. What I see is, its you storing the data and its you reading the data ? Why XML (or JSON) ??

注意2:当您使用不同的应用程序(比如一个内置的.net和一个内置的java)时,以JSON和XML保存数据是很好的。使用这些开放格式,您可以轻松传递数据。我看到的是,你存储数据和你读数据?为什么选择XML(或JSON)?

UPDATE

For JSON serialization - you could use http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.aspx

对于JSON序列化 - 您可以使用http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.aspx

if this xml never changes then do not read it every time. Read and parse the xml, keep it in memomry, what every way you like (session, static variable, cache).

如果这个xml永远不会改变,那么每次都不要读它。读取并解析xml,将其保存在memomry中,各种方式(会话,静态变量,缓存)。

Why are you not using DB for storing the same information ? One application can write the data in DB and other application would retrieve the data from DB ?

为什么不使用DB存储相同的信息?一个应用程序可以在DB中写入数据,而其他应用程序可以从DB中检索数据?