过去一天我一直在尝试阅读和修改这个xml
<TimeSeriesDocument xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:Test">
<DocumentIdentification v="ZDAE_02417b6a-477a-a-4aa19e1c4712"/>
<TimeSeries>
<Period>
<TimeInterval v="12" />
<Resolution v="PT1H" />
<Interval>
<Pos v="17" />
<Qty v="0" />
</Interval>
<Interval>
<Pos v="19" />
<Qty v="10" />
</Interval>
</Period>
</TimeSeries>
</TimeSeriesDocument>
我想修改期间内每个时间间隔内的每个数量
我一直在使用后代,但我正在努力绕过每个区间并挑选和更改数量
var doc = XDocument.Load(test.xml);
var periods = doc.Descendants("TimeSeriesDocument/TimeSeries").Select(e => new { name = e.Element("Period").Value });
你能帮忙吗?
答案 0 :(得分:1)
循环访问Qty节点并为v属性设置新值的示例控制台应用程序:
class Program
{
static void Main(string[] args)
{
var document = new XmlDocument();
document.Load("Sample.xml");
XmlNamespaceManager nsmgr = new XmlNamespaceManager(document.NameTable);
nsmgr.AddNamespace("ns", "urn:Test");
var intervalNodes = document.SelectNodes("//ns:Interval", nsmgr);
foreach(XmlNode node in intervalNodes)
{
var quantityNode = node.SelectSingleNode("./ns:Qty", nsmgr);
quantityNode.Attributes["v"].Value = "New Value";
}
}
}
答案 1 :(得分:1)
XNamespace ns = "urn:Test";
XElement doc = XElement.Load("file.xml");
var periods = doc.Descendants(ns + "Period");
var intervals = periods.Elements(ns + "Interval");
foreach (var qty in intervals.Elements(ns + "Qty"))
{
qty.Attribute("v").Value = "42";
}