adding new controls in xml

sam

Member
Joined
Sep 8, 2016
Messages
10
Programming Experience
Beginner
Hello Everyone,

I am creating dynamic controls based on xml file in wpf , and I have to save value of controls back to this xml again.
So next time if I ran it ,it will take these control values & show in UI.
Now I am facing issues when someone add new entity in xml file. How efficiently I can recognize that this new entity is added when running application next time & I should create only this control via code.

THanks & Appreciate your help
 

Sheepings

Senior Programmer
Joined
Sep 5, 2018
Messages
652
Location
UK
Programming Experience
10+

Skydiver

Well-known member
Joined
Apr 6, 2019
Messages
644
Location
Virginia Beach, VA
Programming Experience
10+
This thread looks like just a paraphrase of the OP's other thread dynamic control from xml

Anyway, I'm not seeing the value of sorting the previous program state using the JavaScript serializer, and then deserializing it on next run and then comparing it against what might be new XML. Why not just always load from the XML regardless of the previous state? Just always create controls from scratch based on what is in the XML. Is there some feature that you are trying to implement that you have not told us about that is dependent on the previous state?
 

Sheepings

Senior Programmer
Joined
Sep 5, 2018
Messages
652
Location
UK
Programming Experience
10+
Agree. Technically its the same question and should probably be merged.
 

Sheepings

Senior Programmer
Joined
Sep 5, 2018
Messages
652
Location
UK
Programming Experience
10+
I'm not seeing the value of sorting the previous program state using the JavaScript serializer, and then deserializing it on next run and then comparing it against what might be new XML. Why not just always load from the XML regardless of the previous state?
I'm not seen where I suggested that I should compare anything? If anything I'd use the same file, and write over it if something else is added. 😒

If you reread what OP wants to do, there is no better way. The idea offered is the only logical way based on what they wants to do.

If that wasn't clear. OP should write out the settings to XML if a control is added, and when the program starts, it reads the XML for the controls last added. I believe that's what is being suggested by the OP.
 

Skydiver

Well-known member
Joined
Apr 6, 2019
Messages
644
Location
Virginia Beach, VA
Programming Experience
10+
... JavaScriptDeserializer ... and then iterate over the collection or better use IEnumerable and move through each element in your XML file.
I interpreted this to mean:
Step 1: loading from the JSON into a collection (or read from JSON and return IEnumerable); then
Step 2: Move through each element in the XML file.

Since JavaScriptDeserializer is used to read JSON files, and it wouldn't make sense to store XML in a JSON, then what else would you be doing with the JSON and the XML if not to compare them, specially if the OP said:
How efficiently I can recognize that this new entity is added when running application next time & I should create only this control via code.
 

Sheepings

Senior Programmer
Joined
Sep 5, 2018
Messages
652
Location
UK
Programming Experience
10+
OP asked how to go about it, and I gave my advice. If its optional for him to use non-xml, then my suggestion is fitting.

Woops. Ohh 🙄, I see how you got confused. I didn't mean to say xml lol, sorry about that, sorry I had a long night last night, but you should know better; since you've seen me answer this question like 100's times on the dic forum in various languages. Are you seriously trying to tell me what a Deserializer is used for Skydiver or do you think I plucked that though out of my ass at random, like I've never used it before?

I prefer using JavaScript Serializer/Deserializer rather than XmlDocument when faced with working with settings file, I'd always choose the JS S/Deserializer. And I still don't see your point about comparing; when I never mentioned anything to do with comparing anything, nor do i see a reason to compare. Despite the typo, I thought you would have known what I meant. 😒 Ah well..

Maybe you would like a diagram of what I meant instead?
 
Last edited:

Skydiver

Well-known member
Joined
Apr 6, 2019
Messages
644
Location
Virginia Beach, VA
Programming Experience
10+
Yes, I know about your preference to use the JS serializer/deserializer. It is why when you mentioned it and also was talking about XML, I thought that you wanted to do something would both. And since the only thing that would make sense to do if you had both is to try to see what is new in the XML, but not present in the old JSON therefore the need to do some kind of comparison.

I didn't realize that the XML was a typo because: the OP had always been talking about XML for driving his UI; and even is the OP switched to JSON, how would he still discover what changed is there is only one JSON file in play.

Let's say the OP were using JSON to drive his UI. Taking post #1 and substituting JSON for XML, his question becomes:
I am creating dynamic controls based on JSON file in wpf , and I have to save value of controls back to this JSON again.
So next time if I ran it ,it will take these control values & show in UI.
Now I am facing issues when someone add new entity in JSON file. How efficiently I can recognize that this new entity is added when running application next time & I should create only this control via code.
For that to work, the OP would need the old state serialized somehow separate from the JSON where he originally loaded from, then wrote values back into, and which later got modified by the user with a new entity. So again you'll have the old state and the JSON file. How do you find what that new entity is?
 

Skydiver

Well-known member
Joined
Apr 6, 2019
Messages
644
Location
Virginia Beach, VA
Programming Experience
10+
Like you, I also take the same mindset of "why even try to find what changed?" There should be no need to compare. Simply start from scratch and load the controls as defined by the file (be it JSON or XML).
 

Skydiver

Well-known member
Joined
Apr 6, 2019
Messages
644
Location
Virginia Beach, VA
Programming Experience
10+
After stepping away from this for a while, here's an idea: mark the elements that you have seen with a special mark when you save back to the file using your program. If you are using XML, add a special attribute. If you are using JSON, add another member.

The value of the attribute or member should not be something that a regular user can just casually copy-and-paste from an existing element into the new element they are trying to add. The value should be based on the properties of the element that matter to you: name, type, value, etc. This could be as simple as a ROT13 copy of those properties, or more sophisticated like a checksum, CRC, or hash of those properties.

The point of having this attribute/member is that when you next load the file, you then can go through each element. If the element is missing the attribute/member, then it must be something new because the things you've seen before would have the attribute/member. Next recalculate a value based on the properties currently found in the file. If the element's value doesn't match the recalculation, then you know that the user has twiddled the values in the file. You can choose to treat this element as new, or spank the user's hand for modifying stuff that they shouldn't.

(As as aside, I considered going all out and using XML digital signatures and signing each of the elements that you've seen, but as the name would imply, XML digital signatures are only standardized for XML, and I couldn't find a comparable standard for JSON. Probably just weak Google-fu on my part.)
 

Sheepings

Senior Programmer
Joined
Sep 5, 2018
Messages
652
Location
UK
Programming Experience
10+
I just noticed that I actually said XML more than once. Terrible mistake on my part, but I was truly exhausted yesterday as I was working from the night before through to the next day. Your idea in the last post is close to what I was thinking too though, and I think that's likely the best way to go about it. (y)
 
  • Like
Reactions: sam

Sheepings

Senior Programmer
Joined
Sep 5, 2018
Messages
652
Location
UK
Programming Experience
10+
Hello Everyone,

I am creating dynamic controls based on xml file in wpf , and I have to save value of controls back to this xml again.
So next time if I ran it ,it will take these control values & show in UI.
Now I am facing issues when someone add new entity in xml file. How efficiently I can recognize that this new entity is added when running application next time & I should create only this control via code.

THanks & Appreciate your help
If you want to stop a user from manipulating your XML or in my case; JSON, since that is what I prefer to use. You could encrypt the contents of the file so the user reading it won't be able to easily add new components unless they know the decrypted files contents. Encrypting the contents would probably not stop a user from attempting to manipulate your XML, but it might. However, this is the problem with storing settings files locally. They're then subject to be manipulated by those with praying eyes. I wrote the following not in WPF but Winforms. If I was you, I'd keep track of the controls being added like so :
C#:
        public List<MyControl> ControlProperties = new List<MyControl>();
        private List<MyControl> GetControlsOnForm()
        {
            int count = 0;
            MyControl myControl = new MyControl();
            foreach (Control c in Controls)
            {
                myControl.controlIndex = count;
                myControl.controlName = c.Name;
                myControl.controlText = c.Text;
                myControl.controlLocation = string.Concat(c.Location.X, ",", c.Location.Y);
                myControl.controlSize = string.Concat(c.Size.Width, ",", c.Size.Height);

                string serialObj = JsonConvert.SerializeObject(myControl, Formatting.Indented);
                ControlProperties.Add(myControl);

                File.AppendAllText(Path.Combine(Application.StartupPath, "settings.json"), serialObj);
                count++;
            }
            return ControlProperties;
        }
You will need the following to keep track of the controls data being added to the form. Note, that the loop above does not check controls for children controls, and that's something you need to add yourself.
C#:
    public class MyControl
    {
        public int controlIndex { get; set; }
        public string controlName { get; set; }
        public string controlText { get; set; }
        public string controlLocation { get; set; }
        public string controlSize { get; set; }

    }
Maybe in a separate thread, you keep re-reading the file in a constant loop with the contents of the controls info on the file and check for any irregularities, and remove any which do not pertain to match those in your list. Note this will only be useful while your app is running. And for encryption, you can use Rijndael Class (System.Security.Cryptography) to make it a bit harder for anyone trying to add in additional controls by simply editing your XML/JSON file.

Note that I used JSON; but it's not hard to switch one for the other. I just prefer working with JSON when writing settings files. If you don't like how its stylised, you can edit that up where its serialised. The output of the code above is as follows:
JSON:
{
  "controlIndex": 0,
  "controlName": "listBox1",
  "controlText": "",
  "controlLocation": "379,28",
  "controlSize": "409,108"
}{
  "controlIndex": 1,
  "controlName": "panel1",
  "controlText": "",
  "controlLocation": "15,28",
  "controlSize": "358,180"
}{
  "controlIndex": 2,
  "controlName": "label2",
  "controlText": "label2",
  "controlLocation": "12,306",
  "controlSize": "35,13"
}{
  "controlIndex": 3,
  "controlName": "label1",
  "controlText": "label1",
  "controlLocation": "12,278",
  "controlSize": "35,13"
}{
  "controlIndex": 4,
  "controlName": "tb1",
  "controlText": "foo",
  "controlLocation": "0,0",
  "controlSize": "100,20"
}
 
Last edited:
  • Like
Reactions: sam

Sheepings

Senior Programmer
Joined
Sep 5, 2018
Messages
652
Location
UK
Programming Experience
10+
Just a small addition to this while i was busy writing in the properties, I forget to add what type of control in the foreach loop :
C#:
        myControl.controlType = c.GetType().Name;
That will give you the type of control by the name of the control, ie Listbox, TextBox etc, and simply add one other property to the class :
C#:
        public string controlType { get; set; }
 
Top Bottom