相关疑难解决方法(0)

如何读取嵌入式资源文本文件

如何使用嵌入式资源(文本文件)StreamReader并将其作为字符串返回?我当前的脚本使用Windows窗体和文本框,允许用户查找和替换未嵌入的文本文件中的文本.

private void button1_Click(object sender, EventArgs e)
{
    StringCollection strValuesToSearch = new StringCollection();
    strValuesToSearch.Add("Apple");
    string stringToReplace;
    stringToReplace = textBox1.Text;

    StreamReader FileReader = new StreamReader(@"C:\MyFile.txt");
    string FileContents;
    FileContents = FileReader.ReadToEnd();
    FileReader.Close();
    foreach (string s in strValuesToSearch)
    {
        if (FileContents.Contains(s))
            FileContents = FileContents.Replace(s, stringToReplace);
    }
    StreamWriter FileWriter = new StreamWriter(@"MyFile.txt");
    FileWriter.Write(FileContents);
    FileWriter.Close();
}
Run Code Online (Sandbox Code Playgroud)

.net streamreader embedded-resource

643
推荐指数
12
解决办法
53万
查看次数

将xml文件加载到内存中

我需要在内存中加载一个xml文件,这样我就可以从不同的表单中多次访问它.xml采用以下格式:

  <Slides>
    <Slide>
      <Name>Name 1</Name>
      <Value>Value 1</Value>
      <Type>Type 1</Type>
    </Slide>
    <Slide>
      <Name>Name 2</Name>
      <Value>Value 2</Value>
      <Type>Type 2</Type>
    </Slide>
  </Slides>
Run Code Online (Sandbox Code Playgroud)

我不想使用数据库来存储变量.有没有其他方法将数据存储在内存中?现在我正在为每个幻灯片和流媒体播放器使用txt文件,我敢肯定这不是最好的选择.

编辑:我添加了这段代码,但是我每次都能获得幻灯片而无需再次阅读xml文件吗?

        var slides = from s in XElement.Load("slides.xml").Elements("Slide")
                      select s;
        foreach (var slide in slides)
        {
            //code
        }
Run Code Online (Sandbox Code Playgroud)

c# xml

2
推荐指数
1
解决办法
6606
查看次数

标签 统计

.net ×1

c# ×1

embedded-resource ×1

streamreader ×1

xml ×1