0

所以我有这个自动生成的 user.config 文件。我想要做的是反序列化 XML 并将我需要的数据放入属性中。该类只是 XML 数据应该进入的属性。现在属性都显示为空。不知道还需要什么来获取数据。我应该使用 XmlSerializer.Deserialize 吗?或者是别的什么?解决这个问题的最佳方法是什么?

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <configSections>
        <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
            <section name="Project1.Setting.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
            <section name="Project1.Echos.Tires.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
            <section name="Project1.Echos.Axles.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
        </sectionGroup>
    </configSections>
    <userSettings>
            <Project1.Setting.Properties.Settings>
            <setting name="RPMs" serializeAs="String">
                <value />
            </setting>
            <setting name="Body" serializeAs="String">
                <value>ironcast</value>
            </setting>
            <setting name="WindResistance" serializeAs="String">
                <value>-1.23</value>
            </setting>            
        </Project1.Setting.Properties.Settings>
        <Project1.Echos.Tires.Properties.Settings>
            <setting name="Size" serializeAs="String">
                <value>22.5</value>
            </setting>
            <setting name="IsAirless" serializeAs="String">
                <value>False</value>
            </setting>
            <setting name="premium" serializeAs="String">
                <value>True</value>
            </setting>            
        </Project1.Echos.Tires.Properties.Settings>
        <Project1.Echos.Axles.Properties.Settings>
            <setting name="Material" serializeAs="String">
                <value>Aluminum Mix</value>
            </setting>
            <setting name="Breakpoint" serializeAs="String">
                <value />
            </setting>
            <setting name="Torque" serializeAs="String">
                <value>182.7</value>
            </setting>
            <setting name="Length" serializeAs="String">
                <value>73.5</value>
            </setting>            
        </Project1.Echos.Axles.Properties.Settings>
    </userSettings>
</configuration>

该类只是 XML 数据应该进入的属性。

        [Serializable, XmlRoot(ElementName = "configuration")]
        public class UserConfig
        {
           public UserConfig() { }

                   private bool ReadUserConfigXML(string xmlFilePath = "")
    {
        if (xmlFilePath != string.Empty) { _xmlFilePath = xmlFilePath; }
        var xmlSerializer = new XmlSerializer(typeof(UserConfig));
        bool userConfigExists = File.Exists(_xmlFilePath);
        if (userConfigExists)
        {
            using (FileStream xmlFileStream = new FileStream(_xmlFilePath, FileMode.Open))
            {
                _userConfig = (UserConfig)xmlSerializer.Deserialize(xmlFileStream);
            }
        }

        return userConfigExists;
    }            

           [XmlElement("userSettings")]
           public UserSettingsProperties UserSettingsPropertiesData {get; set;}
        
           public class UserSettingsProperties
           {
                    public UserSettingsProperties()
                    {
        
                    }
        
                    [XmlElement("Project1.Setting.Properties.Settings")]
                    public SettingsProperties SettingsPropertiesData { get; set; }
                    [XmlElement("Project1.Echos.Tires.Properties.Settings")]
                    public TiresProperties TiresPropertiesData { get; set; }
                    [XmlElement("Project1.Echos.Axles.Properties.Settings")]
                    public AxlesProperties AxlesPropertiesData { get; set; }
        
                    public class SettingsProperties
                    {
                       public SettingsProperties()
                       {
        
                       }
        
                       public string RPMs {get; set;}
                       public string Body {get; set;}
                       public string WindResistance {get; set;}
                    }
           }
       }
4

1 回答 1

1

这是一个XDocument用于读取一个项目设置的示例。

var xml = XDocument.Load("XMlFile1.xml");
var results = xml.Descendants("userSettings")
                    .Select(x => x.Element("Project1.Setting.Properties.Settings"))
                    .Descendants("setting")
                    .Select(x=>new
                    {
                        name = x.Attribute("name").Value,
                        serializeAs = x.Attribute("serializeAs").Value,
                        value = x.Element("value").Value
                    });
于 2021-07-23T14:22:10.130 回答