0
我必須在C#中讀出所有依賴於用戶名的設置(參數:'name'和'visible'),並按照'name'的順序將其存儲到列表中然後「可見」等。C#通過XElement從XML文件中讀取取決於值的內容
所以列表的內容應該是(在客戶端的用戶名:「服務」):
Name-of-Service-Setting-1
True1S
Name-of-Service-Setting-2
True2S
Name-of-Service-Setting-3
True3S
但我只得到了第一個孩子(在客戶端的用戶名:「服務」):
Name-of-Service-Setting-1
True1S
這裏是我的C#-code:
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 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 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("Setting").Element("Name").Value);
settingsList.Add(p.Element("Setting").Element("Visible").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)
{
// Readout the Settings of Client:
Console.WriteLine("Readout all the settings of client " + "'" + username + "':");
List<string> resultList1 = new List<string>();
resultList1 = ReadUserSettings(username, clients);
if (resultList1.Count != 1)
{
for (int i = 0; i < resultList1.Count(); i++)
{
Console.WriteLine(resultList1.ElementAt(i));
}
}
else Console.WriteLine(resultList1.ElementAt(0));
Console.WriteLine("\n- - - - - End-Of-File- - - - - ");
Console.Read();
}
static void Main()
{
var asm = Assembly.GetExecutingAssembly();
var textStream = asm.GetManifestResourceStream("LinqToXML_Example.AccessData.xml");
var xmlReader = new XmlTextReader(textStream);
XElement clients = XElement.Load(xmlReader);
Query("Service", clients);
}
}
}
這是我AccessData.xml文件:
<Client>
<Username>Administrator</Username>
<Password>Admin-Password</Password>
<Settings>
<Setting>
<Name>Name-of-Admin-Setting-1</Name>
<Visible>True1A</Visible>
</Setting>
<Setting>
<Name>Name-of-Admin-Setting-2</Name>
<Visible>True2A</Visible>
</Setting>
<Setting>
<Name>Name-of-Admin-Setting-3</Name>
<Visible>True3A</Visible>
</Setting>
</Settings>
</Client>
<Client>
<Username>Service</Username>
<Password>Service-Password</Password>
<Settings>
<Setting>
<Name>Name-of-Service-Setting-1</Name>
<Visible>True1S</Visible>
</Setting>
<Setting>
<Name>Name-of-Service-Setting-2</Name>
<Visible>True2S</Visible>
</Setting>
<Setting>
<Name>Name-of-Service-Setting-3</Name>
<Visible>True3S</Visible>
</Setting>
</Settings>
</Client>
<Client>
<Username>Customer</Username>
<Password>Customer-Password</Password>
<Settings>
<Setting>
<Name>Name-of-Customer-Setting-1</Name>
<Visible>True1C</Visible>
</Setting>
<Setting>
<Name>Name-of-Customer-Setting-2</Name>
<Visible>True2C</Visible>
</Setting>
<Setting>
<Name>Name-of-Customer-Setting-3</Name>
<Visible>True3C</Visible>
</Setting>
</Settings>
</Client>