读取XML文件(仅来自项目)

本文关键字:项目 XML 文件 读取 | 更新日期: 2023-09-27 17:59:26

我必须从本地Visual Studio 2013项目结构中读取一个XML文件(包含现有内容),但不能从硬盘驱动器目录中读取(正如大多数教程/指南中所提到的,我在过去2个小时内读过…)。在读取过程之后,我必须在其中搜索一些标记名。

我必须这样做,因为要求是,指定的本地XML文件不应该在生成过程之后出现在输出目录中。。。

那么我该怎么做呢?

=================================================

这是我的AccessData.xml-file(在项目目录中:"LinqToXML_Example/AccessData.xml"):

<?xml version="1.0" encoding="utf-8"?>
  <Clients>

    <Client>
        <Username>Administrator</Username>
        <Password>Admin-Password</Password>
        <Settings>
          <Item1>Admin-Setting 1</Item1>
          <Item2>Admin-Setting 2</Item2>
          <Item3>Admin-Setting 3</Item3>
        </Settings>
    </Client>

    <Client>
        <Username>Service</Username>
        <Password>Srv-Password</Password>
        <Settings>
          <Item1>Srv-Setting 1</Item1>
          <Item2>Srv-Setting 2</Item2>
          <Item3>Srv-Setting 3</Item3>
        </Settings>
    </Client>

    <Client>
        <Username>Customer</Username>
        <Password>Cust-Password</Password>
        <Settings>
          <Item1>Cust-Setting 1</Item1>
          <Item2>Cust-Setting 2</Item2>
          <Item3>Cust-Setting 3</Item3>
        </Settings>
    </Client>

  </Clients>

这是我的程序.cs文件:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
using System.Reflection;
namespace LinqToXML_Example
{
    public class Program
    {

        public static string SearchForUsername(string username, XElement clients)
        {
            var userName = from p in clients.Elements()
                            where p.Element("Username").Value == username
                            select p.Element("Username").Value;
            foreach (var p in userName)
            {
                return p + " found!";
            }
            return "User '" + username + "' does not exist!";
        }

        public static bool IsUsernameExisting(string username, XElement clients)
        {
            var userName = from p in clients.Elements()
                            where p.Element("Username").Value == username
                            select p.Element("Username").Value;
            foreach (var p in userName)
            {
                return true;
            }
            return false;
        }

        public static string SearchForUsernamePassword(string username, XElement clients)
        {
            if (IsUsernameExisting(username, clients))
            {
                var password = from p in clients.Elements()
                                where p.Element("Username").Value == username
                                select p.Element("Password").Value;
                foreach (var p in password)
                    return "The password of client " + username + " is: " + p;
            }
            return "Cannot get the username's password, because of a wrong Username!";
        }

        public static List<string> ReadUserSettings(string username, XElement clients)
        {
            List<string> settingsList = new List<string>();

            if (IsUsernameExisting(username, clients))
            {
                var setting = from s in clients.Elements()
                                where s.Element("Username").Value == username
                                select s.Element("Settings");
                foreach (var p in setting)
                {
                    settingsList.Add(p.Element("Item1").Value);
                    settingsList.Add(p.Element("Item2").Value);
                    settingsList.Add(p.Element("Item3").Value);
                }
                return settingsList;
            }

            var errorMsg = "Cannot get the username's settings, because of a wrong Username!";
            settingsList.Add(errorMsg);
            return settingsList;
        }


        public static void Query(string username, XElement clients)
        {
            // Search for specific Username:
            Console.WriteLine("Search for Client " + "'" + username + "' (based on Usernames):");
            string result = SearchForUsername(username, clients);
            Console.WriteLine(result);

            Console.WriteLine("");

            // Search for Password of Client:
            Console.WriteLine("Search for Password of Client " + "'" + username + "':");
            result = SearchForUsernamePassword(username, clients);
            Console.WriteLine(result);

            Console.WriteLine("");

            // Readout the Settings of Client:
            Console.WriteLine("Readout the Settings of Client " + "'" + username + "':");
            List<string> resultList = new List<string>();
            resultList = ReadUserSettings(username, clients);
            if (resultList.Count != 1)
            {
                for (int i = 0; i < resultList.Count(); i++)
                {
                    var itemcounter = i + 1;
                    Console.WriteLine("Item" + itemcounter + ": " + resultList.ElementAt(i));
                }
            }
            else Console.WriteLine(resultList.ElementAt(0));

            Console.Read();
        }


        static void Main()
        {
            var asm = Assembly.GetExecutingAssembly();
  // Bug fix: "AccessData.xml" has to be changed to: "LinqToXML_Example.AccessData.xml" 
            var textStream = asm.GetManifestResourceStream("AccessData.xml");
       // Bug fix: Has to be changed to: var xmlReader = new XmlTextReader(textStream);
            var xmlReader = new XmlReader(textStream);
            XElement clients = XElement.Load(xmlReader);
            Query("Administrator", clients);
        }
    }
}

我已将AccessData.xml文件的属性更改为:

Build Action = "Resource" // Bug fix: Has to be changed to: 'Embeded Resource'
Copy to output directory = "Never"

不幸的是,我无法访问我的xml文件:(

读取XML文件(仅来自项目)

大致扫描了代码后,以下行似乎出现了问题:

var textStream = asm.GetManifestResourceStream("AccessData.xml");

默认情况下,资源名称以项目的默认名称空间为前缀。此外,文件所在的项目文件夹也包含在名称中。因此,在您的情况下,资源名称很可能是"LinqToXML_Example.AccessData.xml"而不是"AccessData.xml"。

此外,您应该将构建操作设置为嵌入式资源

如果您想查找项目文件中包含的所有资源名称,可以使用以下代码并在调试时检查返回值:

string[] names = asm.GetManifestResourceNames();

有关详细信息,请参阅此链接。