Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 读取大型1GB xml文件_C#_Readxml - Fatal编程技术网

C# 读取大型1GB xml文件

C# 读取大型1GB xml文件,c#,readxml,C#,Readxml,我需要打开一个大的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"> <AddressInf

我需要打开一个大的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>

2010-10-29T19:53:32
/级别3>
2010-10-29T19:53:32
/级别3>
类似这样的东西

        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();
        }
string lastTag=”“;
字符串newNode=“\r\n\r\n\r\n”;
整数偏移=5;
使用(FileStream xmlstream=newfilestream)(
@“test.xml”,
FileMode.Open,
FileAccess.ReadWrite,
文件共享(无)
{
//到达appx位置,假设最后一个标记是
//文件中的最后一项。相应地调整偏移量
//满足你的需要
Seek(-(lastTag.Length+offset),SeekOrigin.End);
//检查-我们是否在>
而(xmlstream.ReadByte()!=“>”)
;
//编写我们的xml代码
xmlstream.Write(
Encoding.ASCII.GetBytes(newNode),
0,newNode.Length);
//重写最后一个标记
xmlstream.Write(
Encoding.ASCII.GetBytes(“\r\n”+lastTag+“\r\n”),
0,lastTag.Length+2);
xmlstream.Close();
}

您想读取文件还是附加到文件中?这似乎相关:(仅包含链接答案)。Yair Nevet,我想打开xelement并将其附加到xdocumentOpen作为流,查找(filesize-23)并将其写入,然后写入