我有下一个xml:
<Histories>
<History>
<Date>2011.11.11 08:45</Date>
<Action>Add</Action>
</History>
<History>
<Date>2011.11.12 08:45</Date>
<Action>Modify</Action>
</History>
<History>
<Date>2011.11.13 08:45</Date>
<Action>Delete</Action>
</History>
<History>
<Date>2011.11.14 08:45</Date>
<Action>Add</Action>
</History>
<History>
<Date>2011.11.15 08:45</Date>
<Action>Modify</Action>
</History>
<History>
<Date>2011.11.16 08:45</Date>
<Action>Delete</Action>
</History>
<History>
<Date>2011.11.17 08:45</Date>
<Action>Add</Action>
</History>
<History>
<Date>2011.11.18 08:45</Date>
<Action>Modify</Action>
</History>
<History>
<Date>2011.11.19 08:45</Date>
<Action>Delete</Action>
</History>
<History>
<Date>2011.12.20 08:45</Date>
<Action>Modify</Action>
</History>
</Histories>
我需要使用Action(添加/修改/删除)获取最后一个节点。我能怎么做?
示例:
添加2011.11.17 08:45
修改2011.12.20 08:45
删除2011.11.19 08:45
我这样做:/历史/历史/行动[text()='添加'] /../../历史[position()= last()] - 它不起作用。
由于
答案 0 :(得分:1)
$arr = array('Add','Modify','Delete');
foreach ($arr as $action) {
$res = $xPath->query('/Histories/History[Action="'.$action.'"][last()]/Date');
echo "Last $action: " . $res->item(0)->nodeValue . "\n";
}
编辑:以下是紧凑型回复:/Histories/History[Action="Add"][last()]/Date
答案 1 :(得分:1)
使用强>:
/*/History[Action = 'Delete'] [last()]
这会选择具有字符串值History
的子Action
的最后一个"Delete"
元素,并且(History
元素)是该元素的顶级元素的子元素XML文档。
答案 2 :(得分:0)
考虑使用linq to XML
帮助方法:
string GetValue(XDocument xDocument, string action)
{
var xElements = xDocument.Descendants("Action").Where(x => x.Value == action);
var xElement = xElements.Last().Parent;
return xElement.Element("Date").Value;
}
用法:
var xml = @"<Histories>
<History>
<Date>2011.11.11 08:45</Date>
<Action>Add</Action>
</History>
...
<History>
<Date>2011.12.20 08:45</Date>
<Action>Modify</Action>
</History>
</Histories>";
var xDocument = XDocument.Parse(xml);
var lastAdd= GetLast(xDocument, "Add");
var lastModify = GetLast(xDocument, "Modify");
var lastDelete = GetLast(xDocument, "Delete");
string GetLast(XDocument xDocument, string action)
{
var xElements = xDocument.Descendants("Action").Where(x => x.Value == action);
var xElement = xElements.Last().Parent;
return xElement.Element("Date").Value;
}
答案 3 :(得分:-1)
public void insertToDo(string item, string date, string time, string due, string description)
{
XElement xEmp = XElement.Load(@"C://Users//Khulu//Documents//Visual Studio 2012//Projects//AMD//Schedule//ToDo.xml");
//
xEmp.Add(
new XElement("ToDo",
new XElement("Item", item),
new XElement("date", date),
new XElement("time", time),
new XElement("due", due),
new XElement("description", description))
);
xEmp.Save(@"C://Users//Khulu//Documents//Visual Studio 2012//Projects//AMD//Schedule//ToDo.xml");
}
public DataSet displayGrid()`enter code here`
{
DataSet ds = new DataSet();
ds.ReadXml("C://Users//Khulu//Documents//Visual Studio 2012//Projects//AMD//Schedule//ToDo.xml");
return ds;
}
public void deleteXml(string item)
{
XDocument doc = XDocument.Load("C://Users//Khulu//Documents//Visual Studio 2012//Projects//AMD//Schedule//ToDo.xml");
doc.Root.Elements("ToDo")
.Elements("Item")
.Where(l => l.Value == item)
.Select(x => x.Parent)
.Remove();
doc.Save("C://Users//Khulu//Documents//Visual Studio 2012//Projects//AMD//Schedule//ToDo.xml");
}
public void updateTodo(string item, string date, string time, string due, string description)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("C://Users//Khulu//Documents//Visual Studio 2012//Projects//AMD//Schedule//ToDo.xml");
foreach (XmlNode node in xmlDoc.SelectNodes("toDoList/ToDo"))
{
if (node.SelectSingleNode("Item").InnerText == item)
{
node.SelectSingleNode("Item").InnerText = item;
node.SelectSingleNode("date").InnerText = date;
node.SelectSingleNode("time").InnerText = time;
node.SelectSingleNode("due").InnerText = due;
node.SelectSingleNode("description").InnerText = description;
}
}
xmlDoc.Save("C://Users//Khulu//Documents//Visual Studio 2012//Projects//AMD//Schedule//ToDo.xml");
}
}
}