2

我正在尝试确定加载某些配置设置的最佳方式。基本上,我有几个人会登录的应用程序,一旦他们登录,我想加载他们的所有设置(例如:颜色、字体大小、个人记录等)

我正在考虑使用 XML 文件来执行此操作,因为我认为在 .NET 中解析会相当容易,但它似乎比我预期的更困难。

<ProgramSettings>   
  <database file="C:\database.mdb" />    
</ProgramSettings>

<UserSettings>
  <user key="user1">
    <layout color="red" fontsize="5" />
    <data file="C:\test1.txt" />
  </user>

  <user key="user2">
    <layout color="blue" fontsize="2" />
    <data file="C:\test2.txt" />
  </user>

</UserSettings>

注意:由于某些原因,一些代码没有出现,但基本上有主要部分标记为“程序设置”和“用户设置”。编辑:谢谢谁为我解决了这个问题。

无论如何,我想做的是获取“用户密钥”,这将是用户的登录名或其他内容。然后,能够做这样的事情会很好:

String userLogin = "user1";

// returns red
String color = myXMLFile["UserSettings"][userLogin]["layout"]["color"];         

// returns 5
String fontSize = myXMLFile["UserSettings"][userLogin]["layout"]["fontsize"];   

这样的事情可能吗?我所做的所有研究似乎都表明您需要遍历每个值。我想加载整个文件,并直接访问任何元素。

如果您可以编辑以下值,那也会很酷:

myXMLFile["UserSettings"][userLogin]["layout"]["fontsize"] = "green";
4

5 回答 5

5

我建议您使用集成配置模型,这将承担您的所有工作。您可以在设置下的项目设置中找到它。

它根据需要同时具有用户级别(可更改)和应用程序级别(代码中不可更改),您可以通过projectnamespace.Properties.Settings.

msdn(该链接适用于 Visual Studio 2005,但它也适用于任何其他版本)。

于 2010-12-01T16:22:13.507 回答
2

Take a Look at XDocument if you are using .NET 3.5 or higher.

MSDN XDocument

You could do what you are trying to do as this:

XDocument Settings = new XDocument;
Settings = XDocument.Load("XmlFilePath");

//This Section Gets the Program Settings

XElement ProgramSettings = Settings.Element("ProgramSettings");
string DatabaseFile = ProgramSettings.Element("database").Attribute("file").Value.ToString();


//This section gets the logged in users Settings

XElement UserSettings = Settings.Element("UserSettings");

//Get Node For current USer

XElement CurrentUserSettings = UserSettings.Element(loggedInUserName);
string UserColor = CurrentUserSettings.Element("Layout").Attribute("color").Value.ToString();
string USerFontSize = CurrentUserSettings.Element("Layout").Attribute("font").Value.ToString();
string dataFile = CurrentUserSettings.Element("data").Attribute("file").Value.ToString();

That should work for you.

于 2010-12-01T16:27:45.157 回答
1

I wouldnt say its an ideal approach and i cant remember the exact syntax off top of my head but you can use the System.Linq.Xml library, which is part of .net 3.5.

You would first of all load in your xml file which would be something like:

var xDoc = XDocument.Load("MyFile.xml");
var myElementValue = xDoc.Element("UserSettings")
                         .Element("userLogin")
                         .Element("layout")
                         .Element("fontsize")
                         .Value;

My syntax may not be 100% correct and really you should do some validation to make sure your nodes are all there before you read it all out, but I think that should do what you want. You may even be able to do an XPath to just go directly to the node... something like:

var xmlResult = xDoc.XPathSelectElement("UserSettings/UserLogin/Layout/Fontsize").Value;

Look for that namespace for more info, as this is as far as i know the new way to work with XML since .net 3.5.

于 2010-12-01T16:26:03.620 回答
1

I think a fairly comfortable way of dealing with XML files in C# is using Linq to XML.

using (FileStream lStream = new FileStream("ConfigurationSettings.xml", FileMode.Open, FileAccess.Read))
{
     XElement lRoot = XElement.Load(lReader)
     string userLogin = "user1";
     XElement user = lRoot.Element("UserSettings").Elements("user").Where(x => x.Attribute("Key").Value == userLogin).FirstOrDefault();
      if (user != null)
      {
          // returns red
          string color = user.Element("layout").Attribute("color").Value;

          // returns 5
          string fontSize = user.Element("layout").Attribute("fontsize").Value;
      }

}
于 2010-12-01T16:34:07.450 回答
0

Have a look at the System.Xml.XmlDocument class. Specifically the SelectSingleNode method, which will give you an XmlNode which has a Value property that you can write to.

于 2010-12-01T16:25:32.907 回答