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

时间:2013-08-29 15:54:59

标签: c# xpath xpathnavigator

我有一个以下的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,数据和beta。 在C#中使用XPath。谢谢

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

XPathDocument doc= new XPathDocument(XmlReaderdata);
XPathNavigator mes, Nav = doc.CreateNavigator();

foreach(XPathNavigator node in (XPathNodeIterator)Nav.Evaluate("//response/*)
{
            node.Name;
}

3 个答案:

答案 0 :(得分:0)

像这样的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

答案 1 :(得分:0)

您可以使用//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);
}

答案 2 :(得分:0)

您是否尝试获取子节点的名称? 我希望这会有所帮助

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);