0

So I have this auto-generated user.config file. What I'm trying to do is deserialize the XML and put that data I need into properties. The class is just properties the XML data should go into. Right now the properties are all showing null. Don't know what else is needed to get the data. Should I be using XmlSerializer.Deserialize? Or is something else? What's the best way to tackle this problem?

<?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>

The class is just properties the XML data should go into.

        [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;}
                    }
           }
       }
dimitar.bogdanov
  • 387
  • 2
  • 10
On The Net Again
  • 265
  • 4
  • 16
  • 1
    How exactly do you intend to use this? .Net has its system for handling settings, including serialization, and can VS generate corresponding classes. So you might be able to just rename it to app.config, place it in a project and let visual studio do its thing. – JonasH Jul 23 '21 at 14:05
  • I just need to get the data from the xml file and put ultimately put it in a json file. But I right now need a way to extract the data from the xml file. – On The Net Again Jul 23 '21 at 14:16
  • If it is not more data than posted, and is a one-off operation, it will probably be fastest just to do it by hand. – JonasH Jul 23 '21 at 14:22

1 Answers1

1

Here is one example using XDocument to read one project settings.

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
                    });
Dharman
  • 30,962
  • 25
  • 85
  • 135
Krishna Varma
  • 4,238
  • 2
  • 10
  • 25