Binding Xml data to Treeview in code behind

Last post 09-30-2009 10:13 AM by Assis. 2 replies.

Sort Posts:

  • Binding Xml data to Treeview in code behind

    09-29-2009, 1:14 PM
    • Member
      1 point Member
    • Assis
    • Member since 09-29-2009, 1:03 PM
    • Lisbon
    • Posts 4

                XmlDataSource1.Data = xmldt.OuterXml;
                //XmlDataSource1.DataFile = "C:\\TeamFoundationServer\\Vortex\\MAIN\\Sources\\GUIs\\ServicosV2\\ServicosV2\\XML\\ListaAssuntos.xml";
                XmlDataSource1.DataBind();
                XmlDataSource1.TransformFile = "C:\\TeamFoundationServer\\Vortex\\MAIN\\Sources\\GUIs\\ServicosV2\\ServicosV2\\xslt\\Assuntos.xslt";
                
                TreeView1.DataSource = XmlDataSource1;
                TreeNodeBinding binding = new TreeNodeBinding();
                binding.DataMember = "Assunto";
                binding.ValueField = "value";
                binding.TextField = "name";
                TreeView1.DataBindings.Add(binding);
                TreeView1.DataBind();
     

    Greetings!


    Im trying to bind a XmlDocument to a Treeview using a XmlDataSource and transform the data using a xslt file.

    Everything works fine if i use the DataFile and TransformFile and call the files directly like this:

            XmlDataSource1.DataFile = "C:\\Path\\filename.xml";

    XmlDataSource1.DataBind();

    XmlDataSource1.TransformFile = "C:\\Path\\filename.xslt";


    But when i try to use a XmlDocument i get the error:

    Type 'System.Xml.XmlDocument' in Assembly 'System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' is not marked as serializable.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.Runtime.Serialization.SerializationException: Type 'System.Xml.XmlDocument' in Assembly 'System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' is not marked as serializable.


    The code i use in this case is

    .......

      xmldt = (XmlDocument)ViewState["xmlTree"];

    .......     

    XmlDataSource1.Data = xmldt.OuterXml;

           XmlDataSource1.DataBind();

           XmlDataSource1.TransformFile = "C:\\Path\\filename.xslt";   

                

    In both cases i declare the bindings in the source view of the controls and in debbug the xmldt.OuterXml is the correct one.


    Can anyone help me decypher this bug?

  • Re: Binding Xml data to Treeview in code behind

    09-29-2009, 2:45 PM
    Answer
    • Contributor
      4,076 point Contributor
    • whatispunk
    • Member since 03-06-2007, 10:21 PM
    • Winnipeg
    • Posts 870

    Try putting the OuterXml of your XmlDocument into ViewState rather than the XmlDocument object. That's what's causing the serialization error.

    The XmlDataSource has caching enabled on it by default though, so you probably don't need the ViewState. Pretty aweful thing to do anyways, put an entire xml document into Viewstate.

    Remember: mark posts that helped you as the answer to aid future readers

    Why UpdatePanels Are Dangerous
    Why You Should Not Place Your Whole Site In An UpdatePanel
  • Re: Binding Xml data to Treeview in code behind

    09-30-2009, 10:13 AM
    • Member
      1 point Member
    • Assis
    • Member since 09-29-2009, 1:03 PM
    • Lisbon
    • Posts 4

    Just having my first contact with ASP.NET so don't really now how things work.


    Thanks for the help!



Page 1 of 1 (3 items)