使用XmlDocument更新XML中的特定节点



我是管理XML的新手,我读过几篇文章,但当涉及到我正在处理的特定XML时,我感到困惑。有人能帮我做正确的陈述吗?我只是想更新ListStart的值,但在编译时遇到了一个错误。我更新了这个部分:

XmlDocument soapEnvelopeDocument = new XmlDocument();
soapEnvelopeDocument.Load(@"path");
XmlNode myNode = soapEnvelopeDocument.SelectSingleNode("descendant::cet:GetListCustomElement[cet:GetListCustom/cet:ListID='101']");
soapEnvelopeDocument.LastChild.InnerText = sDate;
<soapenv:Header/>
<soapenv:Body>
<cet:GetListCustomElement>
<!--Zero or more repetitions:-->
<cet:GetListCustom>
<cet:ListID>101</cet:ListID>
<cet:ListStart>13.11.2020</cet:ListStart>
</cet:GetListCustom>
</cet:GetListCustomElement>
</soapenv:Body>
</soapenv:Envelope>```

您只提供了一段没有名称空间的xml。使用Xml-linq,您可以获得不带名称空间的元素。参见以下代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = @"c:temptest.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
XElement ListStart = doc.Descendants().Where(x => x.Name.LocalName == "ListStart").FirstOrDefault();
ListStart.SetValue("14.11.2020");
}
}
}

最新更新