【问题标题】:Reading a large 1 GB xml file [duplicate]读取一个大的 1 GB xml 文件 [重复]
【发布时间】:2014-04-28 06:38:24
【问题描述】:

我需要打开一个大型 XML 文件并将 AddressInfo 元素附加到现有文件中。最好和最快的方法是什么?

我的 XML 示例:

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfAddressInfo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <AddressInfo>    
    <Level1></Level1>
    <Level2>2010-10-29T19:53:32</Level2>
    <Level3>/Level3>
    <Level4></Level4>    
  </AddressInfo>
   <AddressInfo>    
    <Level1></Level1>
    <Level2>2010-10-29T19:53:32</Level2>
    <Level3>/Level3>
    <Level4></Level4>    
  </AddressInfo>
</ArrayOfAddressInfo>

【问题讨论】:

  • 您要读取文件还是追加到文件中?
  • 这似乎相关:stackoverflow.com/questions/14498356/…(包含仅链接答案)。
  • Yair Nevet,我想打开 xelement 并将其附加到 xdocument
  • 作为流打开,寻找(文件大小 - 23)并将其写入那里,然后写入

标签: c# readxml


【解决方案1】:

类似的东西

        string lastTag = "</ArrayOfAddressInfo>";
        string newNode = "\r\n<AddressInfo>\r\n<Level1/>\r\n</AddressInfo>";
        int offset = 5;
        using (FileStream xmlstream = new FileStream(
            @"test.xml",
            FileMode.Open,
            FileAccess.ReadWrite,
            FileShare.None))
        {

            // Get to the appx position, assumes the last tag is the
            // last thing in the file.  Adjust the offset accordingly
            // for your needs
            xmlstream.Seek(-(lastTag.Length + offset), SeekOrigin.End);

            // Check - are we at the >
            while (xmlstream.ReadByte() != '>')
                ;

            // Write our bit of xml
            xmlstream.Write(
                Encoding.ASCII.GetBytes(newNode),
                0, newNode.Length);

            // Rewrite the last tag
            xmlstream.Write(
                Encoding.ASCII.GetBytes("\r\n" + lastTag + "\r\n"),
                0, lastTag.Length + 2);
            xmlstream.Close();
        }

【讨论】:

    猜你喜欢
    • 2017-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-24
    • 2013-02-03
    • 2011-12-10
    • 2013-08-15
    • 2012-03-31
    相关资源
    最近更新 更多