我如何更新Xml文件的价值?

时间:2011-02-28 02:04:27

标签: c# xml

我可以为xml文件的某个元素赋值。我的xml文件格式可能是这样的

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

并且,我想将textbox2的文本值更新为“This Is Update Text”。 有没有办法这样做?我使用c#.net 2008.give me right please,please。

此致

因迪

5 个答案:

答案 0 :(得分:1)

XmlDocument将满足您的需求:

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

答案 1 :(得分:1)

只需在XmlDocument中读取文档,之后您必须找到该节点,该节点可能位于类似于以下的路径:

//对象[@类型= “文本”] [1] /属性[@名称= “文本”]

将它放在XmlNode对象中并更新它的InnerText(我认为)。

我没有测试过它,但它应该是这样的。

答案 2 :(得分:0)

首先,您需要使用DOM解析器解析XML文件,使用DOM解析器更新函数更新值,然后保存/导出XML。您应该告诉我们您使用的语言,我们将为您提供最流行解析器的链接。

答案 3 :(得分:0)

在几乎所有现代编程语言中都有一种处理XML的标准方法;在您的语言文档中,您的问题可能有一个单行的答案。 10秒钟的Google搜索找到了“Reading and Writing XML in C#

答案 4 :(得分:0)

    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.