【发布时间】:2018-05-30 22:38:57
【问题描述】:
自过去 2 小时以来我一直在处理这个问题。我有一个 XML 文件,看起来像这样
<catalog>
<captureInfo>
<row>5</row>
<col>5</col>
</captureInfo>
<patientInfo>
<name>XYZ</name>
<detail>details here</detail>
</patientInfo>
<imageData>
<r0c0>
<contrastFlag>true</contrastFlag>
</r0c0>
<imageData>
<catalog>
我想改变 contrastFlag 的值。我试过了,但它不起作用
XDocument xdoc = XDocument.Load(filename)
xdoc.Element("catalog")
.Element("imageData")
.Descendants()
.Where(x => x.Value == "r0c0")
.First()
.SetElementValue("contrastFlag", "newValue");
doc.Save("XMLFile1.xml");
我能知道我哪里出错了吗?正确的方法是什么?
【问题讨论】:
-
将 where 条件更改为
x.Name == "r0c0" -
我收到此错误 - “System.NullReferenceException:对象引用未设置为对象的实例”
-
<imageData>和<catalog>标签没有正确关闭,但在修复之后,这对我有用xdoc.Element("catalog").Element("imageData").Element("r0c0").SetElementValue("contrastFlag", "newValue"); -
从:.Where(x => x.Value == "r0c0") 到:.Where(x => x.Name.LocalName == "r0c0")
标签: c# xml visual-studio linq