XML Parsing for dynamically creating controls

Last post 05-08-2008 6:57 AM by Samu Zhang - MSFT. 5 replies.

Sort Posts:

  • XML Parsing for dynamically creating controls

    04-24-2008, 12:45 AM
    • Loading...
    • margbarje
    • Joined on 04-24-2008, 12:30 AM
    • Posts 27

    Hi

     Having an xml file serving as a structure of website forms, how can I parse the XML file using LINQ and create dynamically controls based on the xml element and attribute?

     

  • Re: XML Parsing for dynamically creating controls

    04-24-2008, 1:39 AM

    Great question, as has two important parts, what is what you are looking for the LINQ parser or the dynamic control creator?

    For linq I just created a post about it http://alpascual.com/blog/al

    http://www.velocityreviews.com/forums/t69624-dynamic-creation-of-user-controls.html

    Cheers
    Al
    My Blog
    Follow me
    Please click on 'Mark as Answer' if this post answered your question!
  • Re: XML Parsing for dynamically creating controls

    04-24-2008, 2:47 AM
    • Loading...
    • margbarje
    • Joined on 04-24-2008, 12:30 AM
    • Posts 27

    Dear Pascual,

     Thanking you for your reply,

    I've a xml file which is actually just a model for creating a form which will permit users filling it.

    Any change in the xml should change the interface form. The XML file is:

     

    <Discussions>
    
      <Issue issueID="" composer="" date="">
        <Proposition composer="" date="" level="">
          <Agreement composer="" date="" level=""></Agreement>
          <Disagreement composer="" date="" level=""></Disagreement>
          <Clarificationrequest composer="" date="" level=""></Clarificationrequest>
        </Proposition>
      </Issue>
    
    </Discussions>
    I want from this structure, to produce a form with a Label and TextField for letting the user enter information for "Proposition","Agreement" and ... elements, 
    and if an element such as "Clarification Request" is removed from xml document, it will be not displayed in the next form.
    I think the only way to this, is that I parse the xml file, and create controls dynamically, and as I've also heared to much that LINQ facilitate xml handling, I'm
    looking for a solution for creating through LINQ dynamically controls based on the XML file.
     Thanks in advance
     
  • Re: XML Parsing for dynamically creating controls

    04-27-2008, 10:18 PM
    Answer

    Hi margbarje ,

    In this scenairo, I think "Linq to xml" will be used for pick out xml nodes and creating xml file.

    But "Linq to xml" is not used for creating dynamically controls.

    About how to using Linq to xml , you can look at these links:

    http://www.asp.net/learn/linq-videos/video-215.aspx

    http://www.asp.net/learn/linq-videos/video-217.aspx

    About how to creating controls dynamically, see my sample,

     

       protected void Page_Load(object sender, EventArgs e)
        {
            string name = "";
            string type = "";
            string value = "";
    
            XmlDocument doc = new XmlDocument();
            doc.Load(Server.MapPath("doc.xml"));
            XmlNode node = doc.DocumentElement.SelectSingleNode("Control");
    
            name = node.Attributes["ControlName"].Value;
            type = node.Attributes["ControlType"].Value;
            value = node.SelectSingleNode("Value").InnerText;
            
            object obj = Assembly.Load("System.Web").CreateInstance("System.Web.UI.WebControls" + "." + type);
            System.Web.UI.WebControls.WebControl target = (System.Web.UI.WebControls.WebControl)obj;
            target.ID = name;
            target.Visible = true;
            System.Web.UI.ITextControl target2 = (System.Web.UI.ITextControl)obj;
            target2.Text = value;
    
    
            this.Controls.Add(target);
        }
      
    <Controls>
      <Control ControlName="label1" ControlType="Label">
        <Value>
          this is value
        </Value>
      </Control>
    </Controls>
     
    Sincerely,
    Samu Zhang
    Microsoft Online Community Support

    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
  • Re: XML Parsing for dynamically creating controls

    05-01-2008, 1:21 PM
    Answer
    • Loading...
    • margbarje
    • Joined on 04-24-2008, 12:30 AM
    • Posts 27

    Dear Samu,

     your example doesn't work. I've an FileNotFoundException for the line:

            object obj = Assembly.Load("System.Web").CreateInstance("System.Web.UI.WebControls" + "." + type);
     
    Could not load file or assembly 'System.Web' or one of its dependencies
     
    Are there assemblies to add? 
     


     

  • Re: XML Parsing for dynamically creating controls

    05-08-2008, 6:57 AM

    Dear margbarje ,

    To run my sample, you need to copy the file System.Web.dll to Bin folder of your application.

     

    Sincerely,
    Samu Zhang
    Microsoft Online Community Support

    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
Page 1 of 1 (6 items)