我从网页加载一个XML文档。我可以从该文档中检索XML元素的值。有没有什么方法可以让我修改这个值,让它也在网页中得到更新?这应该使用c#来完成
我可以在本地更改该值。我还必须在网页XML中更新它
发布于 2019-04-11 18:59:44
您可以像下面这样轻松地做到这一点:
var newValue = ...; //your new value here
XmlDocument xmlDoc = new XmlDocument(); //create xmldocument object
xmlDoc.Load(xmlFile); //load your file
XmlNode node = xmlDoc.SelectSingleNode("Root/Node/Element");
node.Attributes[0].Value = newValue; //setting the value
xmlDoc.Save(xmlFile); //saving the updated document更新答案:
尝试使用XPathNavigator类,
XmlDocument document = new XmlDocument();
document.Load("xml source");
XPathNavigator navigator = document.CreateNavigator();
XmlNamespaceManager manager = new XmlNamespaceManager(navigator.NameTable);
manager.AddNamespace("app", "xmlns"); //second argument is the xmlns on top of your xml file
foreach (XPathNavigator nav in navigator.Select("//app:node", manager))
{
if (nav.Value == "0")
{
nav.SetValue("1");
}
}有关上述课程的更多信息,请访问here
https://stackoverflow.com/questions/55629253
复制相似问题