![](/img/trans.png)
[英]I want to update the xml element's attribute and it's nested elements value within it in C#
[英]How can I update xml file element value within elements
我有以下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;
}
任何幫助將不勝感激。
您實際使用的不是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將其轉換為字符串表示形式。 同樣,我從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();
...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.