如何使用XPath从根标记中获取所有不同的元素标记

本文关键字:元素 XPath 何使用 获取 | 更新日期: 2023-09-27 18:29:27

我有一个下面的xml文件

<root>
  <element1>
    <header>header1</header>
    <tag1>tag1</tag1>
    <response>
      <status>success</status>
      <Data>
        <id>d1</id>
        <test>2</test>
      </Data>
      <Beta>
        <betaid>sdsd</betaid>
        <code>123</code>
        <code>ddd</code>
      </Beta>
    </response>
  </element1>
</root>

我的问题:如何获得"响应"标签下的第一个子元素?即staus、data和beta。在C#中使用XPath。谢谢

我的.net代码在这里,但它不起作用。

XPathDocument doc= new XPathDocument(XmlReaderdata);
XPathNavigator mes, Nav = doc.CreateNavigator();
foreach(XPathNavigator node in (XPathNodeIterator)Nav.Evaluate("//response/*)
{
            node.Name;
}

如何使用XPath从根标记中获取所有不同的元素标记

这样的XPath查询应该可以工作:

//response/*

例如:

var xml = @"<root> ... </root>";
using (StringReader stream = new StringReader(xml))
{
    XPathDocument doc= new XPathDocument(stream);
    XPathNavigator nav = doc.CreateNavigator();
    XPathNodeIterator itor = (XPathNodeIterator)nav.Evaluate("//response/*");
    foreach(XPathNavigator node in itor)
    {
        Console.WriteLine(node.Name);
    }
}

产生输出:

status
Data
Beta

您可以使用//response:

DTO:

public class Response
{
    public Response()
    {
        Data = new List<KeyValuePair<string, string>>();
        Beta = new List<KeyValuePair<string, string>>();
    }
    public string Status { get; set; }
    public List<KeyValuePair<string, string>> Data { get; set; }
    public List<KeyValuePair<string, string>> Beta { get; set; }
}

代码:

var document = XDocument.Parse(data);
var element = document.XPathSelectElement("//response");
var response = new Response();
response.Status = element.Element("status").Value;
foreach(var dataElement in element.Element("Data").Elements())
{
    response.Data.Add(new KeyValuePair<string, string>(dataElement.Name.LocalName, dataElement.Value));
}
foreach(var betaElement in element.Element("Beta").Elements())
{
    response.Beta.Add(new KeyValuePair<string, string>(betaElement.Name.LocalName, betaElement.Value));
}

编辑:部分加载

public Response ParseResponse(XPathDocument document)
{
    var navigator = document.CreateNavigator();
    var iterator = navigator.Select("//response");
    iterator.MoveNext();
    var responseDoc = XDocument.Parse(iterator.Current.OuterXml);
    var element = responseDoc.Element("response");
    var response = new Response();
    response.Status = element.Element("status").Value;
    foreach(var dataElement in element.Element("Data").Elements())
    {
        response.Data.Add(new KeyValuePair<string, string>(dataElement.Name.LocalName, dataElement.Value));
    }
    foreach(var betaElement in element.Element("Beta").Elements())
    {
        response.Beta.Add(new KeyValuePair<string, string>(betaElement.Name.LocalName, betaElement.Value));
    }
    return response;
}
Response response;
using(var reader = XmlReader.Create(stream))
{
    var doc = new XPathDocument(reader);
    response = ParseResponse(doc);
}

您是否正在尝试获取子节点的名称?我希望这能帮助

string xmlstring = "<root><element><response><status>Success</status><Data><id>1</id></Data></response></element></root>";
        XmlDocument doc = new XmlDocument();
        doc.LoadXml(xmlstring);
        XmlNode node = doc.SelectSingleNode("/root/element/response");
        XmlNodeList children = node.ChildNodes;
        foreach(XmlNode i in children )
        Console.WriteLine(i.Name);