0

たとえば、任意の XML ファイルがある場合、XML ファイルの残りの部分を変更せずに、特定のパラメーター/属性のみを変更する方法はありますか? たとえば、次の簡単な例を見てみましょう。

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <!-- The xml file itself contains many other tags -->
  <ConfigSection>
    <GeneralParams>
      <parameter key="TableName" value="MyTable1" />
    </GeneralParams>
  </ConfigSection>
</configuration>

TableNameキーの値を「MyTable2」に更新するにはどうすればよいですか?

4

2 に答える 2

1

XmlDocument を使用する別の方法があります。

            string xml = @"<?xml version='1.0' encoding='UTF-8'?>
                            <configuration>
                              <ConfigSection>
                                <GeneralParams>
                                  <parameter key='TableName' value='MyTable1' />
                                </GeneralParams>
                              </ConfigSection>
                            </configuration>";

            XmlDocument xDoc = new XmlDocument();
            xDoc.LoadXml(xml);

            XmlNode paramter;
            XmlNode root = xDoc.DocumentElement;

            paramter = xDoc.SelectSingleNode("//parameter/@key");
            paramter.LastChild.InnerText = "MyTable2";

            string modifiedxml = xDoc.OuterXml;
于 2013-10-30T08:59:14.430 に答える