XML, C# Windows Forms and one heck of a headache

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • datastreamcowboy
    New Member
    • Nov 2006
    • 6

    XML, C# Windows Forms and one heck of a headache

    Here's the deal I'm relatively new to C# and i got this windows form that i would like to use to edit the data in an XML file. I can use XMLRead and make a tree view that works.. but how do I get let's say the value in <owner> to show up in to the textbox so it can be entered if it is a new datafile or editited if its an old one.

    I got the loading of the XML file working... tested it with the tree view.

    <document>
    <surface>
    <owners>
    <owner id="1">Sum dum guy</owner>
    <address>1070 0 West Addison Chicago, Il</address>
    </owners>
    </surface>
    </document>


    the problem could also be in how I am trying to load the data into the textbox it's self. I tried to copy the node.value to string tOwner. and then use that variable later on down inthe code to be the text int he text box...

    private void InitializeCompo nent()
    {
    .... stuff .....


    this.textOwner. Location = new System.Drawing. Point(511,50);
    this.textOwner. Name="textOwner ";
    this.textOwner. Size = "new System.Drawing. Size(100, 20);
    this.textOwner. TabIndex = 3;
    this.textOwner. Text = txtOwner;


    .... more stuff .....

    }
  • datastreamcowboy
    New Member
    • Nov 2006
    • 6

    #2
    ok.. found the problem.... kinda... i got a string to show in the field of the text box... ruled that one out...


    is it better to put the XML file in to a string and read/write it usign XMLReader/XMLWriter or as a dataset?

    Comment

    • datastreamcowboy
      New Member
      • Nov 2006
      • 6

      #3
      ok i have set up XmlReader

      Code:
      while (reader.Read())
      {
           switch (reader.NodeType)
           {
                case XmlNodeType.Element:
                     if (reader.Name == "owner")
                     {
                           tOName = reader.Value;
                     }
      
      // ------ more code here

      ok when i use reader.Name i get the variable to fill the text box with owner, but if i use reader.Value.. i get nothing..... <scratching head>
      Last edited by datastreamcowboy; Dec 1 '06, 03:22 PM. Reason: typo

      Comment

      • datastreamcowboy
        New Member
        • Nov 2006
        • 6

        #4
        ok i got it fixed... i can read my XML document.... and fill in the form.. and here is how:

        the xml document
        Code:
        <? xml>
        <document>
           <surface>
              <detail state="TX" county="Johnson" description="See LTC." />
              <owners>
                 <owner id="1" name="Sum Dum Guy" />
              </owners>
              <abs anumber="A-1314" />
              <tabs>
                 <tab id="1" itype="mineral deed" fdate="03/04/2006" />
                 <tab id="2" itype="warranty deed" fdate="03/01/2006" /> 
              </tabs>        
        </document>

        then here is how t fill out the form...


        Code:
        private void cmdLoad_Click(object sender, EventArgs e)
        {
           XmlDocument document = new XmlDocument();
           document.Load(@"c:\sample-data.xml");          // replace with file name var.
           XmlNodeList owners = document.SelectNodes("/owners/owner");
           foreach (XmlElement owner in owners)
           {
              // fills in the tOwner textBox
              this.tOwner.Text = owner.GetAttribute("name"); 
           }
        
        // Here is the tricky part we are going to gen the textboxes on the fly @ load
        // call the onload method of the base class to ensure the Load
        // event is raised correctly
           base.OnLoad(e);
        
        // get number of rows
           XmlNodest tabs2 = document.SelectNodes("//tabs/tab");
           int x = 0;
           foreach (XmlElement tab2 in tabs2)
           {
              x++; // Row counter
           }
        
           // let's fill the arrays with data
           XmlNodeList tabs= document.SelectNodes("//tabs/tab");
           int c = 0;   // another counter
           string[] itypes = new string[x]; // create array for itypes
           string[] fdates = new string[x]; // create array for fdates
        
           foreach (XmlElement tab in tabs)
           {
              itypes[c] = tab.GetAttribute("itype");
              fdates[c] = tab.GetAttribute("fdate");
              c++;
           }
        
           this.SuspendLayout();
        
           int topPosition = 33;
           int zCounter = 0;
           while (xCounter < x)
           {
              TextBox TextBox = new TextBox();// Creates a new text box
              TextBox.Top = topPosition;           // Top Position of the row
              TextBox.Left = 11;                        // Left Side of the column
              TextBox.Text = itypes[zCounter];   // fills in from the array
              TextBox.Size = new System.Drawing.Size(98,22);  // TB Size
        
              // add it to panel
              panel1.Controls.add(TextBox);
        
             TextBox textFdateBox = new TextBox();
              textFdateBox.Top = topPosition;
              textFdateBox.Left = 115;
              textFdateBox.Text = fdates[zCounter];
              textFdateBox.Size = new System.Drawing.Size(100,22);
              // add it to panel
              panel1.Controls.add(textFdateBox);
        
              zCounter++;
              topPosition+= 24; // drop down for next row
           }
          // resume the the form's layout logic 
          this.ResumeLayout();
        }

        Comment

        Working...