Indi
Indi

Reputation: 191

How I Update Value To Xml File?

can i assign value to some element of xml file.my xml file format may be like this

<Object type="TextBox">
  <Property name="Size">94, 20</Property>  
  <Property name="Name">TextBox1</Property>
  <Property name="Location">70, 30</Property>
  <Property name="TabIndex">1</Property>
  <Property name="Text">This Is Text Box One.</Property>
</Object>
<Object type="TextBox">
  <Property name="Size">94, 20</Property>  
  <Property name="Name">TextBox2</Property>
  <Property name="Location">70, 70</Property>
  <Property name="TabIndex">3</Property>
  <Property name="Text">This Is Text Box Two.</Property>
</Object>
<Object type="Label">
  <Property name="Size">94, 20</Property>  
  <Property name="Name">Label1</Property>
  <Property name="Location">10, 110</Property>
  <Property name="TabIndex">2</Property>
  <Property name="Text">This Is Label One.</Property>
</Object>
<Object type="TextBox">
  <Property name="Size">94, 20</Property>  
  <Property name="Name">TextBox3</Property>
  <Property name="Location">70, 110</Property>
  <Property name="TabIndex">4</Property>
  <Property name="Text">This Is Text Box Three.</Property>
</Object>

and,i want to update text value of textbox2 to "This Is Update Text". Is there any way to do like this?I use c#.net 2008.give me right way,please.

Regards

indi

Upvotes: 0

Views: 569

Answers (5)

Wim Haanstra
Wim Haanstra

Reputation: 5998

Just read the document in an XmlDocument after which you have to find the node, which is probably at a path somewhere similar to:

//Object[@type="textbox"][1]/Property[@name="Text"]

Just put that in an XmlNode object and update it's InnerText (I think).

I haven't tested it, but it should be something like this.

Upvotes: 1

Rudrik
Rudrik

Reputation: 832

    On the Button click write this code


  string sStartupPath = Application.StartupPath;  //the path of ~/bin/debug/
     XmlTextWriter objXmlTextWriter = new XmlTextWriter(sStartupPath + @"\selected.xml", null);
     objXmlTextWriter.Formatting = Formatting.Indented;
     objXmlTextWriter.WriteStartDocument();
     objXmlTextWriter.WriteStartElement("TEST");
     objXmlTextWriter.WriteStartElement("Name"); 
     objXmlTextWriter.WriteStartAttribute("Surname"); //Attribute "Name"
      objXmlTextWriter.WriteString("patel"); //Attribute Value 
      objXmlTextWriter.WriteEndAttribute();
     objXmlTextWriter.WriteString(txtname.Text);
     objXmlTextWriter.WriteEndElement();          //End of Name Element
     objXmlTextWriter.WriteStartElement("Age") ;
     objXmlTextWriter.WriteString(txtage.Text);
     objXmlTextWriter.WriteEndElement();             //End of Age element
     objXmlTextWriter.WriteEndElement();
     objXmlTextWriter.Flush();
     objXmlTextWriter.Close();
    MessageBox.Show("The following file has been successfully created\r\n"
                    + sStartupPath
                    + @"\selected.xml", "XML", MessageBoxButtons.OK,
                    MessageBoxIcon.Information);
    //It will create selected.xml file in to your project/bin/debug folder..

    The out put of the xml file will be like this
    value of txtName.text=ABC
    value of txtAge.text=XYZ

    <?xml version="1.0"?>
    <TEST>
      <Name Surname="patel">ABC</Name>
      <Age>XYZ</Age>
    </TEST>

    Hope this will Help.

Upvotes: 0

Anschel Schaffer-Cohen
Anschel Schaffer-Cohen

Reputation: 911

There is a standard way of dealing with XML in almost any modern programming language; there is probably a one-line answer to your question in the documentation for your language. A 10-second Google search found me "Reading and Writing XML in C#"

Upvotes: 0

Steve Wellens
Steve Wellens

Reputation: 20640

The XmlDocument will do what you need:

http://support.microsoft.com/kb/301233

Upvotes: 1

Zepplock
Zepplock

Reputation: 29175

First you need to parse XML file using DOM parser, update the value using your DOM parser update function and then save/export XML back. You should let us know what language you use and we will give you links to most popular parsers.

Upvotes: 0

Related Questions