Click here to Skip to main content
16,020,253 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I wants to set a xml document as a property, where user can set the values of xml attributes and tags but can not change the xml formate. user will not be allowed to delete or add any node in that xml.

Can any body help me out of this ?
Posted
Comments
PIEBALDconsult 2-Sep-15 0:27am    
It isn't easy, and you'll have to do a bit of coding, but maybe this will provide some inspiration:
http://www.codeproject.com/Articles/196495/XmlNode

[EDIT]
Hmm, reading your question again, I think I misunderstood your needs.
If you want I can remove the answer, if it is not helpful in your situation.


One way I have used a lot is to create a class containing all properties you want to save and restore, including a Serialize and Deserialize method. Then you can use a PropertyGrid and set the instance of your class to the SelectedObject member of the PropertyGrid.
Unless you have complicated structures you use as datatypes this approach is pretty straight forward and easy to get started with.

Example class
C#
using System;
using System.ComponentModel;
using System.IO;
using System.Xml.Serialization;

namespace Test
{
    [Serializable]
    public class PropertyExample
    {
        private string fileName;

        PropertyExample()
        {
            fileName = "";
            Integer = 42;
            Text = "Hello World";
        }

        [DefaultValue(42)]
        public int Integer { get; set; }

        [DefaultValue("Hello World")]
        public string Text { get; set; }

        public static PropertyExample Deserialize(string fileName)
        {
            // Initialize the instance to its default values
            PropertyExample props = new PropertyExample();
            try
            {
                if (File.Exists(fileName))
                {
                    using (Stream str = File.OpenRead(fileName))
                    {
                        XmlSerializer xml = new XmlSerializer(typeof(PropertyExample));
                        props = (PropertyExample)xml.Deserialize(str);
                    }
                }
            }
            catch (Exception ex)
            {
                // Error handling
            }
            finally
            {
                props.fileName = fileName;
            }
            return props;
        }

        public void Serialize()
        {
            using (Stream str = File.OpenWrite(fileName))
            {
                XmlSerializer xml = new XmlSerializer(typeof(PropertyExample));
                xml.Serialize(str, this);
            }
        }
    }
}


Code for calling the class
C#
// Get the properties
PropertyExample props = PropertyExample.Deserialize(@"C:\Temp\PropertyExample.xml");

// Assuming you have a PropertyGrid in your form
propertyGrid1.SelectedObject = props;

// Save the properties
props.Serialize();
 
Share this answer
 
v2
Comments
Maciej Los 2-Sep-15 5:05am    
5ed!
George Jonsson 2-Sep-15 6:17am    
Thanks Maciej.
I would declare the property as XDocument and then for each attribute or element accessible to the user, I would make that access the part of XDocument I wanted to change. Thus the main document structure is only changed where you allow the changes. I would also avoid using XDocument if at all possible and use XElement as my preferred container. It is easier to work with and can handle most of your XDocument needs.
 
Share this answer
 
Comments
vj.negi08 2-Sep-15 1:38am    
Thanks for reply but for XDocument it requires vs3.5 or newer version. In my office they still use 2.0, so i can not use XDocument.
Jim Meadors 2-Sep-15 1:45am    
I'm very sorry to here that. I never learned the older technologies. Good Luck!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900