Only Id becomes Null when loading nodes in C#XML

Asked 2 years ago, Updated 2 years ago, 99 views

I want to foreach inside the Users node of XML to read Id, Key, and Address, but only Id is Null, so I can't
Please let me know how to read Id.
Except I can't read Id, it works, but I'm working on my own while struggling, so there might be some strange things.
Thank you for your advice and suggestions.

<AppSettings>
  <FileFolder>C:\</FileFolder>
  <Sky>0</Sky>
  <Users>
    <UserId="User0001">
      <Key></Key>
      <Address>D:\C#Create\Face Mark_Free Material\img038.png</Address>
    </User>
    <UserId="User0002">
      <Key>2</Key>
      <Address/>
    </User>
    <UserId="User0003">
      <Key>3</Key>
      <Address/>
    </User>
  </Users>
</AppSettings>
 [Serializable()]
public class AppSettings
{
    public string FileFolder {get;set;}
    public byte SKy { get; set; } // Array number of labels
    // User Information
    public List <User>Users {get;set;}
    public class User
    {
        public string Id { get; set; } // ID
        public string key { get; set; } // username
        public string address {get; set; } // user image address
    }

    // Instances of the Settings class
    NonSerialized()
    private static AppSettings_instance;
    [System.Xml.Serialization.XmlIgnore]
    public static AppSettings Instance
    {
        get
        {
            // If_instance is null, create a new instance
            if(_instance==null)
                _instance=new AppSettings();

            return_instance;
        }
        set {_instance=value;}
    }
}

private void MainForm_Load(object sender, EventArgse)
{
    // -- Abbreviated --
    foreach (AppSettings.UserLabel in AppSettings.Instance.Users) // Load Saved User Labels
    {
        if(userLabel.Key!="")
        { 
            UserList.Items.Add(userLabel.Key);
            if(UCount==KeyNum)UserName.Text=userLabel.Key;//Key of the saved UserId
        }
        else
        {
            UserList.Items.Add(userLabel.Id);
            if(UCount==KeyNum)
            {
                UserName.Text=userLabel.Id;
                UserPicture.BackgroundImage=Image.FromFile(userLabel.Address); // Saved UserId Address
            }
        }
    }
}

c# xml

2022-09-30 14:44

1 Answers

Is this article helpful?C# Class design to de-serialize XML tags consisting of attributes and text

public class User
{
    System.Xml.Serialization.XmlAttribute("Id") // Include Attribute for XML Element (Tag)
    public string Id { get; set; } // ID
    public string key { get; set; } // username
    public string address {get; set; } // user image address
}

This post was edited based on @kunif's Comment and posted as Community Wiki.This post was edited based on @kunif's Comment and posted as Community Wiki.


2022-09-30 14:44

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.