我必须从我的本地Visual Studio 2013项目结构中读出一个XML文件(已经存在的内容),而不是从harddrive目录中读出(如大多数教程/指南中所述,我已经阅读了最后2个小时......)在读出过程之后,我必须在其中搜索一些标记名称。
我必须这样做,因为要求是,指定的本地XML文件不应该出现在输出目录中的构建过程之后......
那我怎么能这样做呢?
=============================================== ===================================
这是我的AccessData.xml文件(在项目目录中:" 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>
这是我的Program.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文件:((
答案 0 :(得分:1)
大致扫描了您的代码,以下行似乎是问题所在:
var textStream = asm.GetManifestResourceStream("AccessData.xml");
默认情况下,资源名称以项目的默认命名空间为前缀。此外,包含该文件的项目文件夹也包含在名称中。因此,在您的情况下,资源名称可能是&#34; LinqToXML_Example.AccessData.xml&#34; 而不是&#34; AccessData.xml&#34;。
此外,您应该将构建操作设置为嵌入式资源。
如果要查找项目文件中包含的所有资源名称,可以使用此代码并在调试时检查返回值:
string[] names = asm.GetManifestResourceNames();
有关详细信息,请参阅此link。