Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/295.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# 找到字符串后拆分大型XML文件_C#_Xml_String - Fatal编程技术网

C# 找到字符串后拆分大型XML文件

C# 找到字符串后拆分大型XML文件,c#,xml,string,C#,Xml,String,我所拥有的: 一个大的XML文件,内容将近100万行。内容示例: <etc35yh3 etc="numbers" etc234="a" etc345="date"><something><some more something></some more something></something></etc123> <etc123 etc="numbers" etc234="a" etc345="date"><

我所拥有的:

一个大的XML文件,内容将近100万行。内容示例:

<etc35yh3 etc="numbers" etc234="a" etc345="date"><something><some more something></some more something></something></etc123>
<etc123 etc="numbers" etc234="a" etc345="date"><something><some more something></some more something></something></etc123>
<etc15y etc="numbers" etc234="a" etc345="date"><something><some more something></some more something></something></etc123>

^重复900k行左右(当然内容会改变)

我需要的是:


在XML文件中搜索
“您可以使用LINQ2XML进行复制

XElement doc=XElement.Load("yourXML.xml");
XDocument newDoc=new XDocument();

foreach(XElement elm in doc.DescendantsAndSelf("etc123"))
{
newDoc.Add(elm);
}

newDoc.Save("yourOutputXML.xml");

要完全以文字方式丢弃搜索字符串上方的内容,我不会使用File.ReadAllLines,因为它会将整个文件加载到内存中。请尝试使用File.StreamReader.ReadLine打开并将其包装在StreamReader.Loop中,然后开始写入新的StreamWriter,或者在底层文件流上执行字节复制

下面列出了仅使用StreamWriter/StreamReader执行此操作的示例

//load the input file
//open with read and sharing
using (FileStream fsInput = new FileStream("input.txt", 
    FileMode.Open, FileAccess.Read, FileShare.Read)) 
{
    //use streamreader to search for start
    var srInput = new StreamReader(fsInput);
    string searchString = "two";
    string cSearch = null;
    bool found = false;
    while ((cSearch = srInput.ReadLine()) != null)
    {
        if (cSearch.StartsWith(searchString, StringComparison.CurrentCultureIgnoreCase)
        {
            found = true;
            break;
        }
    }
    if (!found)
        throw new Exception("Searched string not found.");

    //we have the data, write to a new file
    using (StreamWriter sw = new StreamWriter(
        new FileStream("out.txt", FileMode.OpenOrCreate, //create or overwrite
            FileAccess.Write, FileShare.None))) // write only, no sharing
    {
        //write the line that we found in the search
        sw.WriteLine(cSearch);

        string cline = null;
        while ((cline = srInput.ReadLine()) != null)
            sw.WriteLine(cline);
    }
}

//both files are closed and complete

您可以一次执行一行…如果检查每行的内容,则不会使用read结束

FileInfo file = new FileInfo("MyHugeXML.xml");
FileInfo outFile = new FileInfo("ResultFile.xml");

using(FileStream write = outFile.Create())
using(StreamReader sr = file.OpenRead())
{
    bool foundit = false;
    string line;
    while((line = sr.ReadLine()) != null)
    {
        if(foundit)
        {
            write.WriteLine(line);
        }
        else if (line.Contains("<etc123"))
        {
            foundit = true;
        }
    }
}
FileInfo file=newfileinfo(“MyHugeXML.xml”);
FileInfo outFile=newfileinfo(“ResultFile.xml”);
使用(FileStream write=outFile.Create())
使用(StreamReader sr=file.OpenRead())
{
bool foundit=false;
弦线;
而((line=sr.ReadLine())!=null)
{
如果(找到它)
{
write.WriteLine(行);
}

else if(line.Contains)(
有效地丢弃了上面的内容。
如果生成的文件是有效的XML,这意味着什么?@Anirudha喜欢忽略它-aka,不写它(忽略它)如果结果不需要是XML-只需使用ReadToEnd+IndexOf并将字符串的尾部写入新文件…我强烈怀疑您是否希望这样做…如果输出需要是XML-请使用XML API进行读写。我已经编辑了您的标题。请参阅“”,其中的共识是“不,他们不应该”.+1,因为它以良好的注释回答了问题…顺便说一句,字节复制将很难,因为人们不知道编码…如果在XML中指定,StreamReader将无法处理非UTF8/16编码…(怀疑是否有人关心此问题)“AlexeiLevenkov,我同意Re:字节复制,我曾希望有一种方法来与StreamReader寻求,但忽略了缓冲的进行。除非文件很大,或有一个明显的性能要求,我可能会留在流线型。成功。只有一个小的变化。(在“if(cSerach.StartsWith)”行中添加了一个结束括号)。我相信这应该可以做到——感谢大家的帮助