I am sure I will ask several questions before I'm done but here is my first:
Basically I have a WEB application that I would like to be used to create an XML file (because of the structure of an XML file) to organize a cost estimator segregated by different cost sections. Most of the Sections will be exactly the same so if I can get one page to work I think can pretty much copy, paste and modifiy after that.
I have the code to generate the file and the initial tags. I can add tags to it from different pages no problem.
The Problem, begins when I come to a section where there may be a dynamic number of cost records and their corresponding descriptions segregated within a specific section.
I want to be able to add individual costs via a data grid, then once added, display them within the same datagrid. But how can I initially bind to data that doen't exist? I can create the tags with the attributes I need as well as the inner text like so:
[HTML]
<Section2>
<text>Main text area that each section will have. even with no cost tags</text>
<cost value="100.00>this is a cost description</cost>
<cost value=45.89">this is another cost description</cost>
etc......
</Section2>
[/HTML]
How can I display the value attribute of each cost tag and the inner text within a datagrid within each section as I create them.
Here is the code I use to create the main text tag:
Expand|Select|Wrap|Line Numbers
- XmlTextReader reader = new XmlTextReader(fileName);
- XmlDocument doc = new XmlDocument();
- doc.Load(reader);
- reader.Close();
- XmlElement root = doc.DocumentElement;
- XmlElement s1 = doc.CreateElement("Section2");
- s1.InnerXml = "<text>" + this.TextBox1.Text + "</text>".Trim() ;
- root.AppendChild(s1);
- doc.Save(fileName);
- bindGrid() //--the function that binds the data to the grid
Expand|Select|Wrap|Line Numbers
- XmlDocument doc = new XmlDocument();
- doc.Load(fileName);
- XmlNode s2 = doc.SelectSingleNode("/Root/Section2");
- XmlElement cost = null;
- XmlAttribute valattrib = null;
- //Create New cost XML Element
- cost = doc.CreateElement("cost");
- //New Attribute
- valattrib = doc.CreateAttribute("value");
- // Value given for the new attribute
- TextBox tbCostAdd = new TextBox();
- tbCostAdd = (TextBox)e.Item.FindControl("tbCostAdd");
- valattrib.Value = tbCostAdd.Text;
- //Attach the attribute to the XML element
- cost.SetAttributeNode(valattrib);
- //Value given for the cost element
- TextBox tbDescrAdd = new TextBox();
- tbDescrAdd = (TextBox)e.Item.FindControl("tbDescrAdd");
- cost.InnerText = tbDescrAdd.Text;
- s2.AppendChild(cost);
- doc.Save(fileName);
- bindGrid();
- //my bind function
- private void bindGrid()
- {
- DataSet ds = new DataSet();
- ds.ReadXml(fileName); //--read the xml file--//
- DataGrid1.DataSource = ds;
- DataGrid1.DataBind();
- }
I have looked all over the place for code example but none I found quite answer my question. Maybe I need to change how I creating the cost tag, but I need to associate a description with each cost so I set an attribute as the cost value and the inner text as the description.
Any helpful hints or advice would be greatly apprciated!