0

我有以下 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>

我的问题:如何获取“响应”标签下的第一个子元素?即状态、数据和测试版。在 C# 中使用 XPath。谢谢

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

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

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

3 回答 3

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
于 2013-08-29T15:59:15.530 回答
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);
于 2013-08-29T17:26:02.550 回答
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);
}
于 2013-08-29T16:19:33.353 回答