如何更新元素中的xml文件元素值

时间:2013-06-03 18:51:49

标签: c# xml

我有以下XML文件:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<masterController>   <uuid>bcbd01ac-78ff-4656-997b-d4ccc72882d1</uuid>  
<channels>
    <channel>
      <nodeGroups>
        <nodeGroup>
          <analogNode>
            <typeCode>8</typeCode>
            <id>1</id>
            <sdos>
              <sdo>
                <description>Host ID</description>
                <compareLevel>Ignore</compareLevel>
                <datafield>
                  <description>Host ID</description>
                  <compareLevel>Ignore</compareLevel>
                  <offset>2</offset>
                  <size>1</size>
                  <readonly>true</readonly>
                  <isMappedToPdo>false</isMappedToPdo>
                  <ownerNodeSerial>12102904</ownerNodeSerial>
                  <ownerSdoIndex>3</ownerSdoIndex>
                  <data>
                    <value>2</value>
                    <unit></unit>
                    <min>1</min>
                    <max>15</max>
                  </data>
                  <intValue>2</intValue>
                </datafield>
                <index>3</index>
                <totalbytes>3</totalbytes>
              </sdo>
              <sdo>
                <description>Host ID</description>
                <compareLevel>Ignore</compareLevel>
                <datafield>
                  <description>Host ID</description>
                  <compareLevel>Ignore</compareLevel>
                  <offset>2</offset>
                  <size>1</size>
                  <readonly>true</readonly>
                  <isMappedToPdo>false</isMappedToPdo>
                  <ownerNodeSerial>12102905</ownerNodeSerial>
                  <ownerSdoIndex>4</ownerSdoIndex>
                  <data>
                    <value>16</value>
                    <unit></unit>
                    <min>1</min>
                    <max>15</max>
                  </data>
                  <intValue>2</intValue>
                </datafield>
                <index>3</index>
                <totalbytes>3</totalbytes>
              </sdo>
            </sdos>
            <sdos>
              <sdo>
                <description>Host ID</description>
                <compareLevel>Ignore</compareLevel>
                <datafield>
                  <description>Host ID</description>
                  <compareLevel>Ignore</compareLevel>
                  <offset>2</offset>
                  <size>1</size>
                  <readonly>true</readonly>
                  <isMappedToPdo>false</isMappedToPdo>
                  <ownerNodeSerial>12102907</ownerNodeSerial>
                  <ownerSdoIndex>3</ownerSdoIndex>
                  <data>
                    <value>ty</value>
                    <unit></unit>
                    <min>1</min>
                    <max>15</max>
                  </data>
                  <intValue>2</intValue>
                </datafield>
                <index>3</index>
                <totalbytes>3</totalbytes>
              </sdo>
              <sdo>
                <description>Host ID</description>
                <compareLevel>Ignore</compareLevel>
                <datafield>
                  <description>Host ID</description>
                  <compareLevel>Ignore</compareLevel>
                  <offset>2</offset>
                  <size>1</size>
                  <readonly>true</readonly>
                  <isMappedToPdo>false</isMappedToPdo>
                  <ownerNodeSerial>12102906</ownerNodeSerial>
                  <ownerSdoIndex>4</ownerSdoIndex>
                  <data>
                    <value>1.2</value>
                    <unit></unit>
                    <min>1</min>
                    <max>15</max>
                  </data>
                  <intValue>2</intValue>
                </datafield>
                <index>3</index>
                <totalbytes>3</totalbytes>
              </sdo>
            </sdos>
          </analogNode>
        </nodeGroup>
      </nodeGroups>
    </channel>   </channels> </masterController>

我创建了以下代码来更新上面XML文件中的值。但是,我的代码不起作用。谁能告诉我我的代码可能有什么问题?

public void updateXml()
        {
            XElement root = XElement.Load(Server.MapPath("Sample.xml"));
            var value = root.Descendants("datafield")
                .Where(x => (string)x.Element("ownerNodeSerial") == TextBox1.Text.ToString() &&
                            (string)x.Element("ownerSdoIndex") == TextBox2.Text.ToString())
                .Select(x => (string)x.Element("data").Element("value")).FirstOrDefault();


            root.SetElementValue(value, "505");



            root.Save("Sample.xml");

            Process.Start("Sample.xml");

        }
        void UpdateBcName(XElement query, object newValue)
        {
            query.Element("value").SetValue(newValue);
        }

        IEnumerable<XElement> LoadElementWhereReqNameEquals(XElement root, string reqName)
        {
            IEnumerable<XElement> queries = from el in root.Descendants("sdo")
                                            where (from add in el.Elements("datafield")
                                                   where
                                                       (string)add.Element("ownerNodeSerial") == TextBox1.Text &&
                                                       (string)add.Element("ownerSdoIndex") == TextBox2.Text
                                                   select add).Any()
                                            select el;

            return queries;
        }

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

您对XElement.SetElementValue的使用并不是您想要的。您已找到第一个value元素的字符串值(例如“2”),然后您将其用作要更新的元素的名称。我怀疑你真的想要:

var valueElement = root.Descendants("datafield")
    .Where(x => (string)x.Element("ownerNodeSerial") == TextBox1.Text &&
                (string)x.Element("ownerSdoIndex") == TextBox2.Text)
    .Select(x => x.Element("data").Element("value"))
    .FirstOrDefault();

valueElement.Value = 505;

请注意,我已将其设置为int而不是字符串,因为我强烈怀疑它在逻辑上是一个整数 - 让LINQ to XML将其转换为字符串表示形式。同样,我已移除ToStringTextBox1.Text的{​​{1}}来电。我实际上建议将TextBox2.TextTextBox1.Text解析为TextBox2.Text值(或int或其他),然后比较它们:

long