locked
Bind DataGrid With XML File....? RRS feed

  • Question

  • Hello World

    I Need to bind my page's DataGrid with an XML file, XML file will download from server through service. One this i need to require that i dont know how many Columns in XML file....Columns may be differnet on each execution....

    Please let me know the best senario.......ASAP

    Monday, February 27, 2012 1:21 AM

All replies

  • After reading from xml, use the below code to create, class and its properties at runtime

    http://social.msdn.microsoft.com/Forums/en-US/silverlightwcf/thread/f3a4d668-51f0-40c7-af1d-7414ce62afb1//1?how+to+use+dynamic+get+and+set+Property+in+class+of+silverlight

    public  void GenerateAssembly (IEnumerable<XElement> xele)
            {
                try
                {
                    // create a dynamic assembly and module 
                    AssemblyName assemblyName = new AssemblyName();
                    assemblyName.Name = "IptegoDetailsAssenblyClass";
                   
    
                    // create a new type builder
                    
    
                    Dict.Add("AudioIn", CreateProperties(assemblyName, stkpnlCNkAudioIn));
                    Dict.Add("VideoIn", CreateProperties(assemblyName, stkpnlCNkVideoIn));
                    Dict.Add("AudioOut", CreateProperties(assemblyName, stkpnlCNkAudioOut));
                    Dict.Add("VideoOut", CreateProperties(assemblyName, stkpnlCNkVideoOut));
    
                   
                       
    
                        // Loop over the values that we will assign to the properties
    
                        foreach (XElement elem in xele)
                        {
                            try
                            {
                                findele = elem.Element("Dir");
                            }
                            catch (Exception ex)
                            {
                            }
                            bool VideoExist = false;
                            if (elem.Element("VideoExists") != null)
                                VideoExist = true;
    
                            if (findele.Value.ToLower() == "inbound")
                            {
                                FillInboundData(elem, VideoExist);
                            }
                            else if (findele.Value.ToLower() == "outbound")
                            {
                                FillOutboundData(elem, VideoExist);
    
                            }
                        }                   
                    
    
                }
                catch (Exception ex)
                {
                }
            }
    
            private object CreateProperties (AssemblyName assemblyName, List<string> propertieslist)
            {
                object AudioInObject = null;
                AssemblyBuilder assemblybuilder = Thread.GetDomain().DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
                ModuleBuilder module = assemblybuilder.DefineDynamicModule("IptegoDetailsModule");
                TypeBuilder typebuilder = module.DefineType("BindableRowCellCollection", TypeAttributes.Public | TypeAttributes.Class);
                try
                {
                    foreach (string str in list)
                    {
                        string propertyName = str;
    
                        // Generate a private field
                        FieldBuilder field = typebuilder.DefineField("_" + propertyName, typeof(string), FieldAttributes.Private);
                        // Generate a public property
                        PropertyBuilder property =
                            typebuilder.DefineProperty(propertyName,
                                             PropertyAttributes.None,
                                             typeof(string),
                                             new Type[] { typeof(string) });
    
                        // The property set and property get methods require a special set of attributes:
    
                        MethodAttributes GetSetAttr =
                            MethodAttributes.Public |
                            MethodAttributes.HideBySig;
    
                        // Define the "get" accessor method for current private field.
                        MethodBuilder currGetPropMthdBldr =
                            typebuilder.DefineMethod("get_value",
                                                       GetSetAttr,
                                                       typeof(string),
                                                       Type.EmptyTypes);
    
                        // Intermediate Language stuff...
                        ILGenerator currGetIL = currGetPropMthdBldr.GetILGenerator();
                        currGetIL.Emit(OpCodes.Ldarg_0);
                        currGetIL.Emit(OpCodes.Ldfld, field);
                        currGetIL.Emit(OpCodes.Ret);
    
                        // Define the "set" accessor method for current private field.
                        MethodBuilder currSetPropMthdBldr =
                            typebuilder.DefineMethod("set_value",
                                                       GetSetAttr,
                                                       null,
                                                       new Type[] { typeof(string) });
    
                        // Again some Intermediate Language stuff...
                        ILGenerator currSetIL = currSetPropMthdBldr.GetILGenerator();
                        currSetIL.Emit(OpCodes.Ldarg_0);
                        currSetIL.Emit(OpCodes.Ldarg_1);
                        currSetIL.Emit(OpCodes.Stfld, field);
                        currSetIL.Emit(OpCodes.Ret);
    
                        // Last, we must map the two methods created above to our PropertyBuilder to 
                        // their corresponding behaviors, "get" and "set" respectively. 
                        property.SetGetMethod(currGetPropMthdBldr);
                        property.SetSetMethod(currSetPropMthdBldr);                   
    
                       
                    }
                    // Generate our type
                    Type generetedType = typebuilder.CreateType();
    
                    // Now we have our type. Let's create an instance from it:
                    AudioInObject = Activator.CreateInstance(generetedType);
                    return AudioInObject;
                }
                catch (Exception ex)
                {
                }
                return AudioInObject;
            }
    Monday, February 27, 2012 4:21 AM
  • Is that the only way...if yes that what is 

    FillOutboundData(elem, VideoExist);
    in Code
    Monday, February 27, 2012 9:01 AM
  • my xml file information is like

    <FileInfo>
        <FileID>1</FileID>
        <FileNAME>1</FileNAME>
    </FileInfo>

    Tags in FileInfo is Added Dynamically....Not Fixed...Now i Need to bind these XML file to DataGrid....

    Monday, February 27, 2012 9:06 AM