【问题标题】:Get xml node with filter使用过滤器获取 xml 节点
【发布时间】:2012-03-28 05:37:24
【问题描述】:

我有下一个 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>

我需要通过操作(添加/修改/删除)获取最后一个节点。我能怎么做?

例子:

添加 2011.11.17 08:45

修改2011.12.20 08:45

删除2011.11.19 08:45

我这样做:/Histories/History/Action[text()='Add']/../../History[position()=last()] - 它不起作用。

谢谢

【问题讨论】:

  • 是否需要使用 Xpath?你能用 LINQ to XMl 吗?
  • 需要 xpath。我找到了解决方案:/Histories/History [Action ='Delete'][last()] /Date

标签: .net xml xpath


【解决方案1】:

使用

/*/History[Action = 'Delete'] [last()]

这会选择最后一个 History 元素,该元素有一个子 Action,字符串值为 "Delete",并且(History 元素)是 XML 文档顶部元素的子元素。

【讨论】:

    【解决方案2】:
    $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

    【讨论】:

    • 嗯,topicstarter 的帖子标记为 .NET,而您发布了 PHP 示例?
    • 不,我实际上是在发布 xPath。我有机会在 PHP 中尝试它,这就是原因。
    • 我找到了解决方案 :) 见顶部 ;-)
    • @RodyvanSambeek 谢谢 :) 我打算发布 3 个单独的 xPath 字符串,但后来我认为使用循环可能会提供一种更简单的方法。
    【解决方案3】:

    考虑使用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;
    } 
    

    【讨论】:

      【解决方案4】:
      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");
              }
      
          }
      }
      

      【讨论】:

      • 这是什么?这是答案?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多