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

C# 如何更新元素中的xml文件元素值,c#,xml,C#,Xml,我有以下XML文件: <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <masterController> <uuid>bcbd01ac-78ff-4656-997b-d4ccc72882d1</uuid> <channels> <channel> <nodeGroups> <nodeGroup>

我有以下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>

bcbd01ac-78ff-4656-997b-d4ccc72882d1
8.
1.
主机ID
忽略
主机ID
忽略
2.
1.
真的
假的
12102904
3.
2.
1.
15
2.
3.
3.
主机ID
忽略
主机ID
忽略
2.
1.
真的
假的
12102905
4.
16
1.
15
2.
3.
3.
主机ID
忽略
主机ID
忽略
2.
1.
真的
假的
12102907
3.
泰
1.
15
2.
3.
3.
主机ID
忽略
主机ID
忽略
2.
1.
真的
假的
12102906
4.
1.2
1.
15
2.
3.
3.
我已经创建了下面的代码来更新上面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;
        }
public void updateXml()
{
XElement root=XElement.Load(Server.MapPath(“Sample.xml”);
var value=root.substands(“数据字段”)
.Where(x=>(string)x.Element(“ownerNodeSerial”)==TextBox1.Text.ToString()&&
(string)x.Element(“ownerSdoIndex”)==TextBox2.Text.ToString()
.选择(x=>(字符串)x.Element(“数据”).Element(“值”)).FirstOrDefault();
root.SetElementValue(值,“505”);
Save(“Sample.xml”);
Process.Start(“Sample.xml”);
}
void UpdateBcName(XElement查询,对象newValue)
{
query.Element(“value”).SetValue(newValue);
}
IEnumerable LoadElementWhereReqNameEquals(XElement根,字符串reqName)
{
IEnumerable查询=来自root.substands(“sdo”)中的el
其中(来自外接程序el.Elements(“数据字段”)
哪里
(字符串)add.Element(“ownerNodeSerial”)==TextBox1.Text&&
(字符串)add.Element(“ownerSdoIndex”)==TextBox2.Text
选择add).Any()
选择el;
返回查询;
}
任何帮助都将不胜感激。

您使用的并不是您真正想要的。您已经找到了第一个
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将其转换为字符串表示。同样,我已经从
TextBox1.Text
TextBox2.Text
中删除了
ToString
调用。实际上,我建议将
TextBox1.Text
TextBox2.Text
解析为
int
值(或
long
或其他值),然后比较它们:

// TODO: Use int.TryParse instead, to handle invalid input cleanly
int ownerNodeSerial = int.Parse(TextBox1.Text);
int ownerSdoIndex = int.Parse(TextBox2.Text);

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

谢谢,这真的很有帮助,但我如何在我的现有xml文件中保存该值?我需要的是将我更新的值保存在xml文件中。我是这方面的新手。