我正在尝试使用xml在编辑模式下更新我的网格,但是我不能这样做。
我只能编辑一个元素,但不知道如何编辑多个元素
我的xml文件如下所示
<CATALOG>
<CD>
<ID>1</ID>
<Application>Dot Net</Application>
<status>Available</status>
<LastUpdate>02-07-2017</LastUpdate>
<Comments>The Rox for July has been loaded</Comments>
</CD>
<CD>
<ID>2</ID>
<Application>JFORWBK</Application>
<status>Available</status>
<LastUpdate>05-07-2017</LastUpdate>
<Comments>DeLorean data has been loaded</Comments>
</CD>
<CD>
<ID>3</ID>
<Application>Codepress</Application>
<status>Open for Input</status>
<LastUpdate>06-07-2017</LastUpdate>
<Comments>The Rox for July has been loaded</Comments>
</CD>
</catalog>当我在编辑模式下打开网格中的数据时,我可以更新单个元素的id
如果我在隐藏字段中有值,我如何一次更新所有元素。
我可以根据in更新我的xml元素。我在一次中只能更新一个元素。
代码如下:
ID = Request.QueryString["sID"];
XmlDocument xmlDoc = new XmlDocument();
string filepathsUpdate = Server.MapPath("Action.xml");
xmlDoc.Load(filepathsUpdate);
XmlNode node = xmlDoc.SelectSingleNode("/CATALOG/CD[ID=" + ID + "]/Action");
node.InnerText = ssplit[0];
xmlDoc.Save(filepathsUpdate);现在如何在服务器端单击C#中的更新按钮,在编辑模式下更新、和注释。
发布于 2017-07-26 20:06:18
为什么不使用循环逐个更新它们,就像这样。
将所有隐藏字段值放在Ids列表中。然后使用循环来更新XML。
List<int> Ids = new List<int>();
Ids.Add(1);
for (int i = 0; i < Ids.Count; i++)
{
ID = Request.QueryString["sID"];
XmlDocument xmlDoc = new XmlDocument();
string filepathsUpdate = Server.MapPath("Action.xml");
xmlDoc.Load(filepathsUpdate);
XmlNode node = xmlDoc.SelectSingleNode("/CATALOG/CD[ID=" + Ids[i].ToString() + "]/Action");
node.InnerText = ssplit[0];
xmlDoc.Save(filepathsUpdate);
}发布于 2017-07-26 20:21:07
在LINQ to XML中,它非常简单:
var id = Request.QueryString["sID"];
XDocument doc = XDocument.Load("Action.xml");
var catalogDescendants = doc.Descendants("CATALOG");
foreach (var cd in catalogDescendants)
{
//you can update the id here for whatever you want
cd.Element("ID").Value = id;
}发布于 2017-07-26 20:04:11
您可以使用XmlNodeList,对该列表中的每个XmlNode进行迭代,然后执行更新。
如果我没记错的话,它应该是这样的
foreach(XmlNode node in XmlDoc.selectNodes("nodename")){node.innerText = "updated value here";}https://stackoverflow.com/questions/45326315
复制相似问题