简体   繁体   English

如何将值从 XML 文件复制到 C# 中的变量

[英]How to copy values from XML file to variables in c#

I have an *.XMl file like this :我有一个 *.XMl 文件,如下所示:

<?xml version="1.0" encoding="utf-8" ?> 
- <!-- User settings
  --> 
- <Settings>
  <Session_File>C:\programs\Notepad++Portable\help.html</Session_File> 
  <Training_catalogue>C:\Windows\Cursors\aero_ew.cur</Training_catalogue> 
  <Users_ID>C:\Windows\_default.pif</Users_ID> 
  <File_with_badge_ID>C:\Windows\PFRO.log</File_with_badge_ID> 
  <Session_Folder>C:\Program Files</Session_Folder> 
  <PDF_Folder>C:\Program Files\GRETECH\GomPlayer\logos</PDF_Folder> 
  </Settings> 

I would like put each "path" to a variable.我想把每个“路径”放到一个变量中。 For example "String user_id = C:\\Windows_default.pif" I have a following code to read an XML file.例如“String user_id = C:\\Windows_default.pif”我有以下代码来读取 XML 文件。

//Read values from xml file
XElement xelement = XElement.Load("settings.xml");
IEnumerable<XElement> employees = xelement.Elements();
// Read the entire XML
foreach (var employee in employees)
{
   Maybe in this place I have to write some code
}

Please help me请帮我

You could use a Dictionary<string,string> in this case which keys would be element names and values are paths:在这种情况下,您可以使用Dictionary<string,string> ,其中键是元素名称,值是路径:

var settings = XDocument.Load("settings.xml").Root
               .Elements()
               .ToDictionary(x => x.Name, x => (string)x);

Then you can access each path by it's element name.For example: settings["Users_ID"] will return C:\\Windows\\_default.pif然后你可以通过它的元素名称访问每个路径。例如: settings["Users_ID"]将返回C:\\Windows\\_default.pif

If those are static fields in the XML, you can serialize to a class using DataContractSerializer (or XMLSerializer)...如果这些是 XML 中的静态字段,则可以使用 DataContractSerializer(或 XMLSerializer)将其序列化为类...

using System.Runtime.Serialization;
using System.IO;

(also need to add the reference to System.Runtime.Serialization) (还需要添加对 System.Runtime.Serialization 的引用)

[DataContract]
public class Settings
{
    [DataMember]
    public string Session_File { get; set; }
    [DataMember]
    public string Training_catalogue { get; set; }
    [DataMember]
    public string Users_ID { get; set; }
    [DataMember]
    public string File_with_badge_ID { get; set; }
    [DataMember]
    public string Session_Folder { get; set; }
    [DataMember]
    public string PDF_Folder { get; set; }

    public static Settings ReadSettings(string Filename)
    {
        using (var stream = new FileStream(Filename, FileMode.OpenOrCreate))
            try
            {
                return new DataContractSerializer(typeof(Settings)).ReadObject(stream) as Settings;
            }
            catch { return new Settings(); }
    }
    public void Save(string Filename)
    {
        using (var stream = new FileStream(Filename, FileMode.Create, FileAccess.Write))
            new DataContractSerializer(typeof(Settings)).WriteObject(stream, this);
    }
    public Settings()
    {
        //defaults
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM