XML and a DataGrid

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hodgesp
    New Member
    • Mar 2007
    • 7

    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.




    .
  • dorinbogdan
    Recognized Expert Contributor
    • Feb 2007
    • 839

    #2
    See these articles:
    Creating a XML document and
    Manipulate XML File Data.

    Comment

    • hodgesp
      New Member
      • Mar 2007
      • 7

      #3
      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

      Code:
      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.
      Code:
      <Root>
       <section1>
         <text> this is Section 1 text</text>
       </section1>
       <section2>
           <text> this is some text </text>
       </section2>
       [B]<cost value="99.99"> this is cost descr text</cost>
       <cost value="100.25"> this is cost descr text 2</cost>[/B]
       <section3>
        <text> section 3 text </text>
       </section3>
      </Root>
      What I want is: (Note Section2)

      Code:
      <Root>
       <section1>
         <text> this is Section 1 text</text>
       </section1>
       <section2>
           <text> this is some text </text>
           [B]<cost value="99.99"> this is cost descr text</cost>
           <cost value="100.25"> this is cost descr text 2</cost>[/B] 
       </section2>
       <section3>
         <text> section 3 text </text>
       </section3>
       </Root>

      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.

      Comment

      • dorinbogdan
        Recognized Expert Contributor
        • Feb 2007
        • 839

        #4
        Try this code (but update doc.Load(...) and doc.Save(...) with your path/file):
        Code:
        	XmlDocument doc = new XmlDocument();
        	doc.Load(@"d:\temp\doc.xml");
        	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
        	valattrib.Value = "99.99";
        	// Attach the attribute to the XML element
        	cost.SetAttributeNode(valattrib);
        	// Value given for the cost element
        	cost.InnerText = "this is cost descr text";
        	s2.AppendChild(cost);
        
        	// Create New cost XML Element 
        	cost = doc.CreateElement("cost");
        	// New Attribute 
        	valattrib = doc.CreateAttribute("value");
        	// Value given for the new attribute
        	valattrib.Value = "100.25";
        	// Attach the attribute to the XML element
        	cost.SetAttributeNode(valattrib);
        	// Value given for the cost element
        	cost.InnerText = "this is cost descr text 2" ;
        	s2.AppendChild(cost);
        
        	doc.Save(@"d:\temp\doc2.xml");

        Comment

        • dorinbogdan
          Recognized Expert Contributor
          • Feb 2007
          • 839

          #5
          Hi,
          Did you succeed to solve the problem ?
          If yes, please let me know, in order to close the thread.
          Thanks,
          Dorin.

          Comment

          • hodgesp
            New Member
            • Mar 2007
            • 7

            #6
            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.

            Comment

            Working...