0

次の 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;
        }

どんな助けでも大歓迎です。

4

1 に答える 1