使用C#读取大型XML文件 [英] Reading large XML files with C#

查看:107
本文介绍了使用C#读取大型XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何从桌面读取XML文件并将其放入字符串中?

I would like to know how can I read a XML file from my desktop and put it into a string?

这是我的XML:

<smallusers>
    <user id="1">
        <name>John</name>
        <motto>I am john, who are you?</motto>
    </user>
    <user id="2">
        <name>Peter</name>
        <motto>Hello everyone!</motto>
    </user>
</smallusers>
<bigusers>
    <user id="3">
        <name>Barry</name>
        <motto>Earth is awesome</motto>
    </user>
</bigusers>

我想存储每个用户,但仍要检测他们的大小,有没有办法

I want to store each user, but still detect if their small or big, is there a way to do this?

在降级之前,您可能要检查Google,因为我做了研究,但一无所获。

Before you downrate this, you might want to check google because I did research, but found nothing.

推荐答案


在降级之前,您可能要检查google,因为我
进行了研究,但一无所获

"Before you downrate this, you might want to check google because I did research, but found nothing"

您什么也没找到,因为您不知道要搜索的内容,而且您的XML无效,您需要将其括在<$ c $中c> rootElement 。然后,您需要做的第一件事就是从桌面读取该文件(如果存在)。

You found nothing because you don't know what you are searching for, also your XML is invalid, you need to enclose it in a rootElement. Then the first thing you need to do is read this file from the desktop if it exists.

您可以根据需要检查大小,并确定大小是否过大,即使这并不重要。我高度怀疑您的XML文件大小将超过5 GB。如果需要,那么.Net程序中的单个对象都不能超过2GB,最好的方法是在64位计算机上进行1,073,741,823。

You can check the size if you wish at that time and determine if this is "too large" even though it doesn't really matter. I highly doubt your XML file will be 5+ GB in size. If it is then you need an alternative, no single object in a .Net program may be over 2GB, the best you could do is 1,073,741,823 on a 64bit machine.

对于超大的XML文件(大于1.0 GB的任何文件),请按Jon Skeet所述将XmlReader和LINQ结合使用此处

For very large XML files, anything above 1.0 GB, combine XmlReader and LINQ as stated by Jon Skeet here:


如果文档特别大,则可以结合使用XmlReader和$ b通过以流式方式为每个
外部元素从XmlReader创建XElement来将$ b LINQ转换为XML:这使您可以完成LINQ to XML的大部分
转换工作,但仍然仅任何时候都需要一小部分
的内存中的文档。

If your document is particularly huge, you can combine XmlReader and LINQ to XML by creating an XElement from an XmlReader for each of your "outer" elements in a streaming manner: this lets you do most of the conversion work in LINQ to XML, but still only need a small portion of the document in memory at any one time.

对于小型XML文件,任何东西1.0 GB或更低的DOM坚持如下所示。

话虽如此,您需要了解的是 Serialization 反序列化表示平均值。

With that said, what you need is to learn what Serialization and Deserialization mean.

序列化将对象实例转换为

反序列化将XML文档转换为对象实例。

Deserialize convert an XML document into an object instance.

您也可以使用JSON,二进制等代替XML。

Instead of XML you can also use JSON, binary, etc.

在您的情况下,可以这样做 XML 文档>反序列化到对象中,以便在代码中使用。

In your case this is what can be done to Deserialize this XML document back into an Object in order for you to use in your code.

首先修复XML并为其指定根。

First fix up the XML and give it a Root.

<?xml version="1.0" encoding="UTF-8"?>
<DataRoot>
    <smallusers>
        <user id="1">
            <name>John</name>
            <motto>I am john, who are you?</motto>
        </user>
        <user id="2">
            <name>Peter</name>
            <motto>Hello everyone!</motto>
        </user>
    </smallusers>
    <bigusers>
        <user id="3">
            <name>Barry</name>
            <motto>Earth is awesome</motto>
        </user>
    </bigusers>
</DataRoot>

然后在C#中创建根类,您可以通过复制您的Visual Studio 2012+直接生成该类XML,然后转到编辑-粘贴特殊,但我喜欢使用: XML到C#类生成器

Then create the root class in C#, you may generate this directly in Visual Studio 2012+ by copying your XML and going to Edit - Paste Special, but I like to use: XML to C# Class Generator

这是生成C#后代码的样子XML的根类,希望它能帮助您更好地理解它。

Here is what your code would look like after you generate the C# Root Class for your XML, hope it helps you understand it better.

using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

namespace ConsoleApplication1
{
    public class Program
    {
        [XmlRoot(ElementName = "user")]
        public class User
        {
            [XmlElement(ElementName = "name")]
            public string Name { get; set; }
            [XmlElement(ElementName = "motto")]
            public string Motto { get; set; }
            [XmlAttribute(AttributeName = "id")]
            public string Id { get; set; }
        }

        [XmlRoot(ElementName = "smallusers")]
        public class Smallusers
        {
            [XmlElement(ElementName = "user")]
            public List<User> User { get; set; }
        }

        [XmlRoot(ElementName = "bigusers")]
        public class Bigusers
        {
            [XmlElement(ElementName = "user")]
            public User User { get; set; }
        }

        [XmlRoot(ElementName = "DataRoot")]
        public class DataRoot
        {
            [XmlElement(ElementName = "smallusers")]
            public Smallusers Smallusers { get; set; }
            [XmlElement(ElementName = "bigusers")]
            public Bigusers Bigusers { get; set; }
        }


        static void Main(string[] args)
        {
            string testXMLData = @"<DataRoot><smallusers><user id=""1""><name>John</name><motto>I am john, who are you?</motto></user><user id=""2""><name>Peter</name><motto>Hello everyone!</motto></user></smallusers><bigusers><user id=""3""><name>Barry</name><motto>Earth is awesome</motto></user></bigusers></DataRoot>";

            var fileXmlData = File.ReadAllText(@"C:\XMLFile.xml");
            var deserializedObject = DeserializeFromXML(fileXmlData);
            var serializedToXML = SerializeToXml(deserializedObject);

            //I want to store each user, but still detect if their small or big, is there a way to do this?
            foreach (var smallUser in deserializedObject.Smallusers.User)
            {
              //Iterating your collection of Small users? 
              //Do what you need here with `smalluser`.
              var name = smallUser.Name; //Example...
            }


            Console.WriteLine(serializedToXML);
            Console.ReadKey();
        }

        public static string SerializeToXml(DataRoot DataObject)
        {
            var xsSubmit = new XmlSerializer(typeof(DataRoot));

            using (var sw = new StringWriter())
            {
                using (var writer = XmlWriter.Create(sw))
                {
                    xsSubmit.Serialize(writer, DataObject);
                    var data = sw.ToString();
                    writer.Flush();
                    writer.Close();
                    sw.Flush();
                    sw.Close();
                    return data;
                }
            }
        }

        public static DataRoot DeserializeFromXML(string xml)
        {
            var xsExpirations = new XmlSerializer(typeof(DataRoot));
            DataRoot rootDataObj = null;
            using (TextReader reader = new StringReader(xml))
            {
                rootDataObj = (DataRoot)xsExpirations.Deserialize(reader);
                reader.Close();
            }
            return rootDataObj;
        }
    }
}

这篇关于使用C#读取大型XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆