473,387 Members | 1,578 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

XML and a DataGrid

Hidy Ho Neighbors!

I just started a new c# web project. I am going to create an XML file via user input. I have been working with .net and c# for about a year but this is my first forray into xml. I need some help.

I am able to create the XML file insert tags with text values and even attributes via text boxes.

My issue is that I get to one of the sections I'm creating and I need to insert new data in tags that don't exisit in the xml file as yet, from a datagrid. The dataGrid has 4 columnms. Two button columns and two text box columns.

I need to input cost values in one of the text columns and a text description in the other. How do I pass that to the XML file. I want the Cost value to be an attribute and the text to be the description. I will need to add a dynamic number of rows. this is the format I thought would work. Depending on how many rows the users enters.

<cost value="100.01"> This is text to describ the cost value</cost>
<cost value="200.02"> This is text to describ the next cost value</cost>
......more if needed

Then I will want to be able to load it back from the XML file later for editing.




.
Mar 6 '07 #1
5 2506
dorinbogdan
839 Expert 512MB
See these articles:
Creating a XML document and
Manipulate XML File Data.
Mar 6 '07 #2
I read those articles, but they didn't answer all my questions. for one: How do I add Child Nodes From a DataGrid into and XML document under a specific node. I tried

Expand|Select|Wrap|Line Numbers
  1. XmlNodeList s1 = doc.GetElementsByTagName("Sections2");
But I can't seem to use it so I canj insert the child nodes within that node as a parent.

What I get is this.
Expand|Select|Wrap|Line Numbers
  1. <Root>
  2.  <section1>
  3.    <text> this is Section 1 text</text>
  4.  </section1>
  5.  <section2>
  6.      <text> this is some text </text>
  7.  </section2>
  8.  <cost value="99.99"> this is cost descr text</cost>
  9.  <cost value="100.25"> this is cost descr text 2</cost>
  10.  <section3>
  11.   <text> section 3 text </text>
  12.  </section3>
  13. </Root>
  14.  
What I want is: (Note Section2)

Expand|Select|Wrap|Line Numbers
  1. <Root>
  2.  <section1>
  3.    <text> this is Section 1 text</text>
  4.  </section1>
  5.  <section2>
  6.      <text> this is some text </text>
  7.      <cost value="99.99"> this is cost descr text</cost>
  8.      <cost value="100.25"> this is cost descr text 2</cost> 
  9.  </section2>
  10.  <section3>
  11.    <text> section 3 text </text>
  12.  </section3>
  13.  </Root>
  14.  

You see I'm creating a cost estimating wizard and I need to be able to add the main text within each section and if needed cost Child Nodes, with text descriptions within the sections.
Mar 6 '07 #3
dorinbogdan
839 Expert 512MB
Try this code (but update doc.Load(...) and doc.Save(...) with your path/file):
Expand|Select|Wrap|Line Numbers
  1.     XmlDocument doc = new XmlDocument();
  2.     doc.Load(@"d:\temp\doc.xml");
  3.     XmlNode s2 = doc.SelectSingleNode("/Root/section2");
  4.     XmlElement cost = null;
  5.     XmlAttribute valattrib = null;
  6.     // Create New cost XML Element 
  7.     cost = doc.CreateElement("cost");
  8.     // New Attribute 
  9.     valattrib = doc.CreateAttribute("value");
  10.     // Value given for the new attribute
  11.     valattrib.Value = "99.99";
  12.     // Attach the attribute to the XML element
  13.     cost.SetAttributeNode(valattrib);
  14.     // Value given for the cost element
  15.     cost.InnerText = "this is cost descr text";
  16.     s2.AppendChild(cost);
  17.  
  18.     // Create New cost XML Element 
  19.     cost = doc.CreateElement("cost");
  20.     // New Attribute 
  21.     valattrib = doc.CreateAttribute("value");
  22.     // Value given for the new attribute
  23.     valattrib.Value = "100.25";
  24.     // Attach the attribute to the XML element
  25.     cost.SetAttributeNode(valattrib);
  26.     // Value given for the cost element
  27.     cost.InnerText = "this is cost descr text 2" ;
  28.     s2.AppendChild(cost);
  29.  
  30.     doc.Save(@"d:\temp\doc2.xml");
  31.  
Mar 7 '07 #4
dorinbogdan
839 Expert 512MB
Hi,
Did you succeed to solve the problem ?
If yes, please let me know, in order to close the thread.
Thanks,
Dorin.
Mar 21 '07 #5
Sorry I haven't gotten back to this in so long.

The Code Worked as far as creating the correct structure. Thank You Very Much for that. I need to be able to bind the Data to a Data Grid for editing though.
Jun 26 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Ashish Shridharan | last post by:
Hi All I have been trying to add a control to the header cell of a datagrid on my ASP.NET page. These controls are defined in the HTML as ASP.NET web controls. They are being added into the...
3
by: Bill C. | last post by:
Hello, I know this has been discussed a lot already because I've been searching around for information the last few weeks. I'm trying to implement a DataGridComboBoxColumn class. I've found...
5
by: BBFrost | last post by:
Win2000 ..Net 1.1 SP1 c# using Visual Studio Ok, I'm currently in a "knock down - drag out" tussle with the .Net 1.1 datagrid. I've come to realize that a 'block' of rows highlighted within...
2
by: pei_world | last post by:
I want to implement a key hit with enter to dropdown a combobox that is in the datagrid. in this case I need to override its original behaviours. I found some codes from the web. Does anyone know...
1
by: Rick | last post by:
Hello all, I hope all is well with you. I am having a seriously difficult time with this problem. Allow me to set up the problem. I have a System.Web.UI.Page with the following controls...
3
by: CVerma | last post by:
Hi, I have an embedded datagrid within a datalist. I am not able to perfrom paging in the datagrid. Any ideas? Here is my code: Here is my Simplegrid.cs file: using System; using...
2
by: CSL | last post by:
I am using the DataGrid in a Windows Application, how can I adjust the widths of each column individually.
7
by: Dave | last post by:
Are there any add-on products or samples available that can do the following in an vb.net datagrid I want to compare 2 rows in a datagrid - one row from one database and another row for another...
9
by: rn5a | last post by:
A Form has a DataGrid which displays records from a SQL Server 2005 DB table. Users can modify the records using this DataGrid for which I am using EditCommandColumn in the DataGrid. This is the...
2
by: =?Utf-8?B?Y3JlYXZlczA2MjI=?= | last post by:
I have a nested datagrid in a xaml file, the parent datagrid loads the vendor information and the details loads the documents for that vendor in a datagrid. Everything is working fine until I click...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.