Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/338.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#中使用另一个文件从一个文件中删除字符串?_C# - Fatal编程技术网

在c#中使用另一个文件从一个文件中删除字符串?

在c#中使用另一个文件从一个文件中删除字符串?,c#,C#,我有两个文件,即file.txt和database.xml file.txt看起来像 Istanbul Moscow London Saint Petersburg Berlin Madrid Kiev Rome Paris <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <city-group> <city>Paris</city> <city>Lond

我有两个文件,即file.txt和database.xml

file.txt看起来像

Istanbul
Moscow
London
Saint Petersburg
Berlin
Madrid
Kiev
Rome
Paris
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<city-group>
    <city>Paris</city>
    <city>London</city>
    <city>Boulder</city>
    <city>Mumbai</city>
    <city>Quebec</city>
    .....
    .....
</city-group>
database.xml看起来像

Istanbul
Moscow
London
Saint Petersburg
Berlin
Madrid
Kiev
Rome
Paris
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<city-group>
    <city>Paris</city>
    <city>London</city>
    <city>Boulder</city>
    <city>Mumbai</city>
    <city>Quebec</city>
    .....
    .....
</city-group>
由于Paris和London都存在于database.xml中的节点city,因此它们将从file.txt中删除。
最快的方法是什么?

使用
System.IO.file.ReadAllLines()
读取字符串数组中文本文件的所有条目。将XML加载到
XElement
中。然后使用LINQ查找不在
XElement
子元素中的数组元素。我也可以发布代码,但应该很简单

编辑 这基本上可以为您做到:

var FileEntries = System.IO.File.ReadAllLines("TEXT_FILE_PATH");
var XMLEntries = XElement.Load("XML_FILE_PATH").Elements().Select(xx => xx.Value);
var list = FileEntries.Where(e => !XMLEntries.Contains(e));

使用
System.IO.file.ReadAllLines()
读取字符串数组中文本文件的所有条目。将XML加载到
XElement
中。然后使用LINQ查找不在
XElement
子元素中的数组元素。我也可以发布代码,但应该很简单

编辑 这基本上可以为您做到:

var FileEntries = System.IO.File.ReadAllLines("TEXT_FILE_PATH");
var XMLEntries = XElement.Load("XML_FILE_PATH").Elements().Select(xx => xx.Value);
var list = FileEntries.Where(e => !XMLEntries.Contains(e));

这是一个满足您需要的代码

  • 为城市列表创建一个类,并将database.xml文件反序列化为一个对象
  • 逐行读取file.txt,并使用从database.xml读取的城市列表检查每一行
  • 如果城市名称不包含在城市列表中,请将其附加到字符串生成器中
  • 将数据环生成器内容写入file.txt

    使用System.IO; 使用System.Linq; 使用系统文本; 使用System.Xml.Serialization

    命名空间控制台应用程序1 { 班级计划 { 静态void Main(字符串[]参数) { StringBuilder sb=新的StringBuilder()

    }


    • 以下是您需要的代码

      • 为城市列表创建一个类,并将database.xml文件反序列化为一个对象
      • 逐行读取file.txt,并使用从database.xml读取的城市列表检查每一行
      • 如果城市名称不包含在城市列表中,请将其附加到字符串生成器中
      • 将数据环生成器内容写入file.txt

        使用System.IO; 使用System.Linq; 使用系统文本; 使用System.Xml.Serialization

        命名空间控制台应用程序1 { 班级计划 { 静态void Main(字符串[]参数) { StringBuilder sb=新的StringBuilder()

        }


        • 以下是完整的解决方案

          [XmlRoot("city-group")]
          public class Citites
          {
              [XmlElement("city")]
              public string[] City { get; set; }
          }
          
          class Program
          {
              static void Main(string[] args)
              {
                  // 1. read all city's names from xml file
                  Citites cities;
                  using (StreamReader reader = new StreamReader("database.xml"))
                  {
                      var xs = new XmlSerializer(typeof(Citites));
                      cities = (Citites)xs.Deserialize(reader);
          
                  }
                  // 2. read current file and open temp file for insert not matched city 
                  string tempFile = Path.GetTempFileName();
                  using (StreamReader rw = new StreamReader("file.txt"))
                  using (var sw = new StreamWriter(tempFile))
                  {
                      {
                          // while of not end
                          while (rw.Peek() > 0)
                          {
                              // read next line
                              string city = rw.ReadLine();
                              if (!cities.City.Contains(city))
                              { 
                                  // the city's name no matched, insert to temp file
                                  sw.WriteLine(city);
                              }
                          }
                      }
                  }
                  // 3. replace the current file with a temp file
                  File.Delete("file.txt");
                  File.Move(tempFile, "file.txt");
              }
          }
          
          如果file.txt非常大,那么立即读取所有行是一个错误的决定。这将加载RAM

          在这个解决方案中,我们逐行读取,不加载整个文件的内容。

          这是完整的解决方案

          [XmlRoot("city-group")]
          public class Citites
          {
              [XmlElement("city")]
              public string[] City { get; set; }
          }
          
          class Program
          {
              static void Main(string[] args)
              {
                  // 1. read all city's names from xml file
                  Citites cities;
                  using (StreamReader reader = new StreamReader("database.xml"))
                  {
                      var xs = new XmlSerializer(typeof(Citites));
                      cities = (Citites)xs.Deserialize(reader);
          
                  }
                  // 2. read current file and open temp file for insert not matched city 
                  string tempFile = Path.GetTempFileName();
                  using (StreamReader rw = new StreamReader("file.txt"))
                  using (var sw = new StreamWriter(tempFile))
                  {
                      {
                          // while of not end
                          while (rw.Peek() > 0)
                          {
                              // read next line
                              string city = rw.ReadLine();
                              if (!cities.City.Contains(city))
                              { 
                                  // the city's name no matched, insert to temp file
                                  sw.WriteLine(city);
                              }
                          }
                      }
                  }
                  // 3. replace the current file with a temp file
                  File.Delete("file.txt");
                  File.Move(tempFile, "file.txt");
              }
          }
          
          如果file.txt非常大,那么立即读取所有行是一个错误的决定。这将加载RAM

          在这个解决方案中,我们逐行读取,不加载整个文件的内容。

          我得到一个错误对象引用,没有设置为对象的实例。在代码的第2行中。我得到一个错误对象引用,没有设置为对象的实例。在代码的第2行中。谢谢你的回复,但对于一个简单的任务来说,这不是有点复杂。谢谢你或者您的回复,但对于一个简单的任务来说,这不是有点复杂吗?
          [XmlRoot(“城市组”)]
          在类之外是什么?是用于在对象由XmlSerializer序列化时设置根元素名称的属性。在您的示例中,它的值是城市组
          [XmlRoot(“城市组”)]
          类外?是用于在XmlSerializer序列化对象时设置根元素名称的属性。在您的示例中,它的值是city group